C/C++教程

BIN2HEX文件转换C语言解析

本文主要是介绍BIN2HEX文件转换C语言解析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1 #ifndef _bin2hex_C_
  2 #define    _bin2hex_C_
  3 
  4 #include "stdio.h"
  5 #include "string.h"
  6 
  7 #define    BIN_DATA_LEN    24
  8 #define    HEX_STR_LEN        64
  9 
 10 static unsigned int start = 0;
 11 static unsigned int length = 0xFFFFFFFF;
 12 static unsigned int begin = 0;
 13 static unsigned int offset = 0;
 14 static char file_name[64];
 15 static FILE* in_file = (FILE*)0;
 16 static FILE* out_file = (FILE*)0;
 17 
 18 static unsigned char checkSum(unsigned int count,unsigned char* p)
 19 {
 20     unsigned char sum=0;
 21     unsigned int i;
 22     
 23     for(i=count;i>0;i--)
 24     {
 25         sum += *p++;
 26     }
 27     sum = ~sum;
 28     sum++;
 29     return sum;
 30 }
 31 
 32 static unsigned int encode(unsigned int count, unsigned char* in, unsigned char* out)
 33 {
 34     const unsigned char intChar[16] = "0123456789ABCDEF";
 35 
 36     unsigned int i;
 37     unsigned char c;
 38 
 39     *out++ = ':';
 40     for(i=count;i>0;i--)
 41     {
 42         c = *in++;
 43         *out++ = intChar[c>>4];
 44         *out++ = intChar[c&0xF];
 45     }
 46     *out++ = '\r';
 47     *out++ = '\n';
 48 
 49     return (1+(count<<1)+2);
 50 }
 51 
 52 static void writeExtendedLinearAddress(void)
 53 {
 54     unsigned int count,k = 0;
 55     unsigned char bin_data[BIN_DATA_LEN];
 56     unsigned char hex_str[BIN_DATA_LEN];
 57 
 58     bin_data[k++] = 0x02;
 59     bin_data[k++] = 0x00;
 60     bin_data[k++] = 0x00;
 61     bin_data[k++] = 0x04;
 62     bin_data[k++] = (unsigned char)((begin>>24)&0xFF);
 63     bin_data[k++] = (unsigned char)((begin>>16)&0xFF);
 64     bin_data[k++] = checkSum(k,bin_data);
 65     count = encode(k,bin_data,hex_str);
 66     fwrite(hex_str,1,count,out_file);
 67 }
 68 
 69 static void writeEndOfFile(void)
 70 {
 71     unsigned int count,k = 0;
 72     unsigned char bin_data[BIN_DATA_LEN];
 73     unsigned char hex_str[BIN_DATA_LEN];
 74 
 75     bin_data[k++] = 0x00;
 76     bin_data[k++] = 0x00;
 77     bin_data[k++] = 0x00;
 78     bin_data[k++] = 0x01;
 79     bin_data[k++] = checkSum(k,bin_data);
 80     count = encode(k,bin_data,hex_str);
 81     fwrite(hex_str,1,count,out_file);
 82 }
 83 
 84 static void writeDataRecord(unsigned int len,unsigned char* p)
 85 {
 86     #define IGNOR_DATA    0xFF
 87     unsigned int count,k = 0;
 88     unsigned char bin_data[BIN_DATA_LEN];
 89     unsigned char hex_str[BIN_DATA_LEN];
 90 
 91     for(count=0;count<len;count++)
 92     {
 93         if(IGNOR_DATA!=*(p+count))
 94         {
 95             break;
 96         }
 97     }
 98     if(count>=len)
 99     {
100         return;
101     }
102     
103     bin_data[k++] = len;
104     bin_data[k++] = (unsigned char)((offset>>8)&0xFF);
105     bin_data[k++] = (unsigned char)(offset&0xFF);
106     bin_data[k++] = 0x00;
107     for(count=len;count>0;count--)
108     {
109         bin_data[k++] = *p++;
110     }
111     bin_data[k++] = checkSum(k,bin_data);
112     count = encode(k,bin_data,hex_str);
113     fwrite(hex_str,1,count,out_file);
114 }
115 
116 static void writeData(unsigned int count,unsigned char* p)
117 {
118     unsigned int len;
119     if(count)
120     {
121         if((offset+count)<0x10000)
122         {
123             writeDataRecord(count,p);
124             offset += count;
125         }
126         else if((offset+count)==0x10000)
127         {
128             writeDataRecord(count,p);
129             begin += 0x10000;
130             writeExtendedLinearAddress();
131             offset = 0;
132         }
133         else
134         {
135             len = (0x10000-offset);
136             writeDataRecord(len,p);
137             begin += 0x10000;
138             writeExtendedLinearAddress();
139             offset = 0;
140             writeDataRecord(count-len,p+len);
141             offset = count-len;
142         }
143     }
144 }
145 
146 static void bin2Hex(void)
147 {
148     size_t readCount = 0;
149     unsigned int count = 0;
150     unsigned char data[16];
151 
152     writeExtendedLinearAddress();
153     if(fseek(in_file,start,0)>=0)
154     {
155         while(!feof(in_file) && !ferror(in_file))
156         {
157             readCount = fread(data,1,16,in_file);
158             if((count+readCount)>=length)
159             {
160                 readCount = length - count;
161                 writeData(readCount,data);
162                 readCount = 0;
163                 count = length;
164                 break;
165             }
166             writeData(readCount,data);
167             count += readCount;
168         }
169     }
170     writeEndOfFile();
171 }
172 
173 static int text2Data(char* str,unsigned int* data)
174 {
175     unsigned int i,len,temp;
176     char c;
177 
178     len = strlen(str);
179     if(len>8 || len<=0)
180     {
181         return -1;
182     }
183     temp = 0;
184     for(i=len;i>0;i--)
185     {
186         temp <<= 4;
187         c = *str++;
188         if(c>='0' && c<='9')
189         {
190             temp += c-'0';
191         }
192         else if(c>='a' && c<='f')
193         {
194             temp += c-'a'+10;
195         }
196         else if(c>='A' && c<='F')
197         {
198             temp += c-'A'+10;
199         }
200         else
201         {
202             return -1;
203         }
204     }
205 
206     *data = temp;
207     return 0;
208 }
209 
210 static int parseParameter(char* str)
211 {
212     unsigned int data;
213     
214     if(str[0]=='-' && str[1]=='s')
215     {
216         if(text2Data(str+2,&data)<0)
217         {
218             return -1;
219         }
220         start = data;
221     }
222     else if(str[0]=='-' && str[1]=='l')
223     {
224         if(text2Data(str+2,&data)<0)
225         {
226             return -1;
227         }
228         length = data;
229     }
230     else if(str[0]=='-' && str[1]=='b')
231     {
232         if(text2Data(str+2,&data)<0)
233         {
234             return -1;
235         }
236         begin = data & 0xFFFF0000;
237         offset = data & 0xFFFF;
238     }
239     else
240     {
241         return -1;
242     }
243     return 0;
244 }
245 
246 static int getParameter(int argc, char* argv[])
247 {
248     unsigned int len;
249     int i;
250 
251     len = strlen(argv[1]);
252     if(len<=4 || len>=(sizeof(file_name)))
253     {
254         return -1;
255     }
256     else
257     {
258         memcpy(file_name,argv[1],len);
259         if(file_name[len-4]!='.'
260         ||((file_name[len-3]!='b')&&(file_name[len-3]!='B'))
261         ||((file_name[len-2]!='i')&&(file_name[len-3]!='I'))
262         ||((file_name[len-1]!='n')&&(file_name[len-3]!='N')))
263         {
264             return -1;
265         }
266     }
267 
268     for(i=2;i<argc;i++)
269     {
270         if(parseParameter(argv[i])<0)
271         {
272             return -1;
273         }
274     }
275     
276     return 0;
277 }
278 
279 int main(int argc, char** argv)
280 {
281     unsigned int len;
282 
283     if(argc<=1 || argc>5 || getParameter(argc,argv)<0)
284     {
285         goto parameter_error;
286     }
287 
288     if((in_file= fopen(file_name,"rb"))==NULL)
289     {
290         goto parameter_error;
291     }
292     len = strlen(file_name);
293     file_name[len-3] = 'h';
294     file_name[len-2] = 'e';
295     file_name[len-1] = 'x';
296     if((out_file= fopen(file_name,"w"))==NULL)
297     {
298         goto parameter_error;
299     }
300     bin2Hex();
301     fclose(in_file);
302     fclose(out_file);
303     return 0;
304 
305     parameter_error:
306         if(in_file)
307         {
308             fclose(in_file);
309         }
310         if(out_file)
311         {
312             fclose(out_file);
313         }
314         printf("wrong parameters or bin file not existed..\r\n");
315         return -1;
316 }
317 
318 #undef _bin2hex_C_
319 #endif    //_bin2hex_C_

 

这篇关于BIN2HEX文件转换C语言解析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!