C文件怎么改写成java文件? 20C

最近遇到一个棘手的问题,老板给了几个.unl格式的文件,文本打开全是乱码,说是要求用java读取解析,给了个C写的解析过程,可我不懂C语言。虽然有格式文档但是怎么解都解不出正确的内容来,哪位大神有能将C语言翻译成java呢?
或者说那位大神能使用java按照格式解析出二进制的内容,可以联系我哦,可以付钱买源码。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "billBinToAsc.h"
#define BILL_VERSION "2017/07/03"
#define BILL_SIZE 337
#define BILL_ASC_SIZE 2048
#define MAX_BILL_COUNT 10000
void getNowTime(char * strNow,size_t size)
{
    time_t Now;
    struct tm * tm_time;
    Now = time(NULL);
    tm_time = localtime(&Now);
    strftime(strNow,size,"%Y-%m-%d %H:%M:%S",tm_time);

}

void printBill(const tag_bill * billdata,char * strBuf)
{
        char temp[BILL_ASC_SIZE];
        time_t t_time;
        struct tm * tm_time;

        *strBuf = 0;

  //话单ID
        sprintf((char *)&temp,"%d-%d|",billdata->call_id1,billdata->call_id2);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->call_num);
        strcat(strBuf,temp);

        //主被叫
        sprintf((char *)&temp,"%s",billdata->caller_no);
        int len = strlen(temp);
        for (int j=0;j<len;j++)
        {
            //if(temp[j] == '\r' || temp[j] == '\n') 
            if(temp[j] == '\r'||temp[j] == '\n')
            {
                temp[j] = '\0';
            }
        }
        len = strlen(temp);
        //temp[len] ='|';
        char temp2[2]="|";
        strcat(temp,temp2);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%s|",billdata->callee_no);
        strcat(strBuf,temp);

        //时间
        t_time = billdata->wait_begin;
        tm_time = localtime(&t_time);
        strftime(temp,BILL_ASC_SIZE,"%Y-%m-%d %H:%M:%S|",tm_time);
        strcat(strBuf,temp);
        t_time = billdata->wait_end;
        tm_time = localtime(&t_time);
        strftime(temp,BILL_ASC_SIZE,"%Y-%m-%d %H:%M:%S|",tm_time);
        strcat(strBuf,temp);
        t_time = billdata->ack_begin;
        tm_time = localtime(&t_time);
        strftime(temp,BILL_ASC_SIZE,"%Y-%m-%d %H:%M:%S|",tm_time);
        strcat(strBuf,temp);
        t_time = billdata->ack_end;
        tm_time = localtime(&t_time);
        strftime(temp,BILL_ASC_SIZE,"%Y-%m-%d %H:%M:%S|",tm_time);
        strcat(strBuf,temp);
        t_time = billdata->call_begin;
        tm_time = localtime(&t_time);
        strftime(temp,BILL_ASC_SIZE,"%Y-%m-%d %H:%M:%S|",tm_time);
        strcat(strBuf,temp);
        t_time = billdata->call_end;
        tm_time = localtime(&t_time);
        strftime(temp,BILL_ASC_SIZE,"%Y-%m-%d %H:%M:%S|",tm_time);
        strcat(strBuf,temp);

        //模块信息
        sprintf((char *)&temp,"%d|",billdata->serviceno);   //operation_type_id
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->trkno);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->trunkgroup_no);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->module_no);
        strcat(strBuf,temp);

        //设备信息
        sprintf((char *)&temp,"%d|",billdata->device_type);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->device_no);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%s|",billdata->devicein);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->call_type_id);
        strcat(strBuf,temp);

        //原因信息
        sprintf((char *)&temp,"%d|",billdata->waitcause);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->releasecause);
        strcat(strBuf,temp);

        //呼叫中心信息
        sprintf((char *)&temp,"%d|",billdata->subccno);
        strcat(strBuf,temp);    
        sprintf((char *)&temp,"%d|",billdata->vdn);
        strcat(strBuf,temp);

        //杂项
        sprintf((char *)&temp,"%d|",billdata->mediatype);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->uvid);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->orgccno);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%u-%u|",billdata->org_call_id1,billdata->org_call_id2);
        strcat(strBuf,temp);
        //原始呼叫信息
        sprintf((char *)&temp,"%s|",billdata->orgcalleeno);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->orgserviceno);
        strcat(strBuf,temp);

        //级别信息
        sprintf((char *)&temp,"%d|",billdata->serccno);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->serservice);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->userlevel);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->usertype);
        strcat(strBuf,temp);

        //杂项
        sprintf((char *)&temp,"%d|",billdata->callincause);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->enterreason);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->leavereason);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo1);
        strcat(strBuf,temp);
        //上个设备信息
        sprintf((char *)&temp,"%d|",billdata->preserviceno);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->predevicetype);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->predeviceno);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%s|",billdata->predevicein);
        strcat(strBuf,temp);

        //技能
        sprintf((char *)&temp,"%d|",billdata->mediainfotype);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->skillid);
        strcat(strBuf,temp);
        //int billinfo2;
        //int billinfo3;
        //int billinfo4;
        //char str1[31];
        //char str2[25];
        //int billinfo5;
        //int billinfo6;

        //后为添加的部分字段:
        sprintf((char *)&temp,"%d|",billdata->locationid);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo2);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo3);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo4);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo5);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->str1);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->str2);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo6);
        strcat(strBuf,temp);
        sprintf((char *)&temp,"%d|",billdata->billinfo7);
        strcat(strBuf,temp);
    //  sprintf((char *)&temp,"%d|",billdata->billinfo8);
        //strcat(strBuf,temp);
    //  sprintf((char *)&temp,"%d|",billdata->billinfo9);
        //strcat(strBuf,temp);
    //  sprintf((char *)&temp,"%d|",billdata->billinfo10);
        //strcat(strBuf,temp);
        //转换结束
        strcat(strBuf,"\n");

}


int main(int argc,char * argv[])
{
  const char * strSourceFileName;
  const char * strTargetFileName;
  const char * strLogFileName = "billConvt.log";

  char strBillAsc[BILL_ASC_SIZE];
  char strNowDate[1000];

  FILE * fd_s,* fd_t,* fd_log;
  tag_bill tagBillData;
  int iReadCount;

  getNowTime(strNowDate,sizeof(strNowDate));

  //command handle

  if(argc != 3){
         printf("%d command error: [usuage] command sourcefilename targetfilename\n",argc);
         return -1;
  }

  //strSourceFileName = argv[1];
  //strTargetFileName = argv[2];


  strSourceFileName = "H:\\UNL\\Prm20190716_0101_000361.unl";//argv[1];
  strTargetFileName = "H:\\parseData\\Prm20190716_0101_000361.dat";// argv[2];
  //fprintf("%s=",argv[0]);
  //open Source file
  fd_log = fopen(strLogFileName,"a");
  if(fd_s == NULL){
        fprintf(fd_log,"[%s]Cannot Open SourceFileName:%s\n",strNowDate,strSourceFileName);
        return -1;
  }

  //key parameter check
  /*
  if(sizeof(tag_bill) < BILL_SIZE){
        fprintf(fd_log,"[%s][error] tag_bill size error![%d][%d]\n",strNowDate,sizeof(tag_bill),BILL_SIZE);
        return -1;
  }
  */
  //open Source file
  fd_s = fopen(strSourceFileName,"r");
  if(fd_s == NULL){
        fprintf(fd_log,"[%s]Cannot Open SourceFileName:%s\n",strNowDate,strSourceFileName);
        return -1;
  }

  //open Target file
  fd_t = fopen(strTargetFileName,"w");
  if(fd_s == NULL){
        fprintf(fd_log,"[%s]Cannot Open TargetFileName:%s\n",strNowDate,strTargetFileName);
        return -1;
  }

  //读数据
  int i = 0;

  for(i=0;i<=MAX_BILL_COUNT && fread((tag_bill*)&tagBillData,BILL_SIZE,1,fd_s) == 1;i++)
  {
    printBill(&tagBillData,(char *)&strBillAsc);
    fprintf(fd_t,"%d|",i);
    fputs(strBillAsc,fd_t);
  }

  //写日志
  fprintf(fd_log,"[%s] Convert SourceFile:%s into TargetFile:%s OK! record:%d\n",\
          strNowDate,strSourceFileName,strTargetFileName,i);

  //关闭文件
  if(fd_s != NULL) fclose(fd_s);
  if(fd_t != NULL) fclose(fd_t);
  if(fd_log != NULL) fclose(fd_log);
  return 0;
}


(下面是定义的集合文件)
#pragma pack(push,1)

typedef struct
{
   int  head;
   int call_id1;
   int call_id2;
   int call_num;
   char caller_no[26];
   char callee_no[26];
   int wait_begin;
   int wait_end;
   int ack_begin;
   int ack_end;
   int call_begin;
   int call_end;
   int serviceno;
   int trkno;//   
   int module_no;//------14
   int device_type;
   int device_no;
   char devicein[26];
   int call_type_id;
   int waitcause;
   int releasecause;
   short subccno;
   short vdn;
   short mediatype;
   int uvid;
   short trunkgroup_no;
   char orgcalleeno[26];
   short orgccno;
   int org_call_id1;
   int org_call_id2;//14
   int orgserviceno;
   short serccno;
   short serservice;
   unsigned char userlevel;//一字节整形 
   unsigned char  usertype;//一字节整形
   short callincause;
   unsigned char enterreason;//一字节整形
   unsigned char leavereason;//一字节整形
   int billinfo1;
   int preserviceno;
   int predevicetype;
   int predeviceno;
   char predevicein[26];
   int mediainfotype;
   short skillid;
   short locationid;
   int billinfo2;
   int billinfo3;
   int billinfo4;
   int billinfo5;
   char str1[31];
   char str2[25];
   short billinfo6;
   unsigned char billinfo7;
   //short billinfo8;
   //unsigned char billinfo9;
   //short billinfo10;
}tag_bill;

#pragma pack(pop)

1个回答

最简单的办法就是
strSourceFileName = "H:\UNL\Prm20190716_0101_000361.unl";//argv[1];
strTargetFileName = "H:\parseData\Prm20190716_0101_000361.dat";// argv[2];
把这个(包括后面的几个)换成argv[1] argv[2],也就是注释里的代码。

然后编译,得到一个exe文件

用Java直接命令调用 Runtime.getRuntime().exec()
xxx.exe uni文件 输出文件
调用以后,输出文件,这个文本文件就是解析以后的内容,因为是文本文件,Java再直接读取。

fanjinruo
梵瑾箬 最终的的目的是生成jar,用java代替c的功能
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐