1 #include "stdio.h"
2 #include "string.h"
3
4 unsigned char ChartoByte(char c)
5 {
6 if(c-'a'>=0 ) return(c-'a'+10);
7 else if(c-'A'>=0 ) return(c-'A'+10);
8 else return(c-'0');
9 }
10
11 unsigned char Char2toByte(char* s)
12 {
13 return (ChartoByte(*s)*16+ChartoByte(*(s+1)));
14 }
15
16
17 int main(int argc,char *argv[])
18 {
19 FILE *fp_hex = NULL;
20 FILE *fp_bin = NULL;
21 char buff[64] = "" ;
22
23 unsigned char length = 0;
24 unsigned short offset = 0; //0~65535
25 unsigned char type = 0;
26 unsigned char checksum = 0;
27 unsigned char i = 0;
28
29 if(argc != 2) return -1;
30
31 printf("Input file: %s\n",argv[1]);
32
33 if((fp_hex=fopen(argv[1],"r"))==NULL)
34 {
35 printf("File open error!!!\n");
36 return -1;
37 }
38
39 i=strlen(argv[1]);
40 argv[1][i-1]='n';
41 argv[1][i-2]='i';
42 argv[1][i-3]='b';
43 printf("Output file: %s\n",argv[1]);
44
45 if((fp_bin=fopen(argv[1],"wb"))==NULL)
46 {
47 printf("File creat error!!!\n");
48 return -1;
49 }
50
51 while(1)
52 {
53 fgets(buff,64,fp_hex);
54
55 if(feof(fp_hex)) break; //文件结束
56 else if(buff[0] != ':') continue; //无效行
57 else if( strcmp(buff,":00000001FF\n") == 0 ) break; //结束行
58 else
59 {
60 length=Char2toByte(&buff[1]);
61 offset=Char2toByte(&buff[3])*256+Char2toByte(&buff[5]);
62 type=Char2toByte(&buff[7]);
63 if(type==0)
64 {
65 fseek(fp_bin,offset,0);
66 for(i=0; i<length; i++)
67 fputc(Char2toByte(&buff[9+2*i]), fp_bin);
68 }
69
70 printf("%s",buff);
71 }
72 }
73
74
75
76 return(0);
77 }