// translate from htx to btx & bin
//15/2/17 2/19
//
#include <stdio.h>
#include <string.h>
FILE *htxfp;
FILE *btkfp;
FILE *binfp;
char htxfname[50];
char btkfname[50];
char binfname[50];
int n;
int c;
int i;
int j;
int k;
int H;
int L;
int x;
//
int hextobin();
//
void main(){
printf("infilename=");
gets(htxfname);
n=strlen(htxfname);
i=0;
while(i<n){
btkfname[i]=htxfname[i];
binfname[i]=htxfname[i];
if(htxfname[i]==0x2e)break;
i++;
}
if(i==n){
strcat(htxfname,".htx");
strcat(btkfname,".btk");
strcat(binfname,".bin");
}
else{
strcat(btkfname,"btk");
strcat(binfname,"bin");
}
printf("infile=%s\n",htxfname);
printf("outfile1=%s\n",btkfname);
printf("outfile2=%s\n",binfname);
htxfp=fopen(htxfname,"rb");
if(!htxfp){printf("%s cannot open\n",htxfname);return;}
btkfp=fopen(btkfname,"wb");
if(!btkfp){printf("%s cannot open\n",btkfname);fclose(htxfp);return;}
binfp=fopen(binfname,"wb");
if(!binfp){printf("%s cannot open\n",binfname);fclose(htxfp);fclose(btkfp);return;}
//
printf("start to convert\n");
i=0;
j=0;
k=0;
while(1){
c=fgetc(htxfp);
//printf("%02X,",c);//test
if(c==0x0d||c==0x0a)continue;
if(c==EOF)break;
H=c;
i++;
//printf("%d,",i);//test
c=fgetc(htxfp);
//printf("%02X,",c);//test
if(c==EOF)break;
i++;
//printf("%d,",i);//test
L=c;
if(!hextobin()){printf("\n***error! not HTX\n\n");break;};
//printf("%02X\n",x);//test
fputc(x,btkfp);
j++;
if(j>4){fputc(x,binfp);k++;}
}
fclose(htxfp);
fclose(btkfp);
fclose(binfp);
if(i==0)printf("\n***error! file is empty\n\n");
printf("%s=%dbytes\n",htxfname,i);
printf("%s=%dbytes\n",btkfname,j);
printf("%s=%dbytes\n",binfname,k);
printf("end\n");
}
//
int hextobin(){
if(H>=0x30 && H<=0x39)x=(H-0x30)*16;
else if(H>=0x41 && H<=0x46)x=(H-0x37)*16;
else return 0;
if(L>=0x30 && L<=0x39)x=x+(L-0x30);
else if(L>=0x41 && L<=0x46)x=x+(L-0x37);
else return 0;
return 1;
}
//
|