Hex文件转Bin文件

您所在的位置:网站首页 hex文件转bin文件工具 Hex文件转Bin文件

Hex文件转Bin文件

2022-05-13 17:42| 来源: 网络整理| 查看: 265

在嵌入式开发中,编译器生成的目标文件一般都是 .hex 文件。 为什么要转换,直接使用hex文件不行吗,可是我在开发过程中一直都是直接生成hex文件,然后进行下载,也没见出错? 在不清楚hex与bin文件的格式时,可能小伙伴会有这样的疑问。需要进行转换的原因是:hex文件中数据记录(record)并不是按照 “起始地址–>终止地址” 这样的顺序进行排列的,由于每行数据都包含起始地址和数据长度,所以hex文件中数据不需要按照地址顺序从低到高进行排列;而Bin文件中的数据则是严格按照地址顺序进行排列的。

首先需要了解hex文件的格式,可以参考官方资料Intel Hexadecimal Object File Format Specification,也可以看我的另一篇blog——HEX文件说明 。

最近做ECU的上位机下载工具,其中一步是将hex文件的内容转成按地址顺序(从低到高)排列的二进制数据(bin文件)。 于是我找了一个转换工具——hex2bin,源码地址。下面我就该工具的整个转换过程进行一些分析。 整个过程主要分为两步: 1. 遍历整个hex文件,找出最小地址和最大地址(也就是起始地址和结束地址),算出数据长度(数据长度=结束地址-起始地址),根据得到的数据长度,分配对应大小的内存(开辟一个数组); 2. 再次遍历整个hex文件,计算每条数据记录中的起始地址与hex文件起始地址的偏移量,按照偏移量将该条数据记录中的数据部分写入第一步的数组中。(这样就实现了按照从低到高的地址顺序排列整个hex文件的数据)。 最后只需要将该数组写出到文件中即可。

首先使用FILE * fopen(const char * path, const char * mode);打开hex文件,然后是第一次遍历,找出起始地址和数据长度。

/* 第一次遍历hex文件,获取地址范围(Lowest_Address和Highest_Address) */ /* get highest and lowest addresses so that we can allocate the right size */ do { unsigned int i; /* Read a line from input file. */ GetLine(Line,Filin); Record_Nb++; /* Remove carriage return/line feed(回车/换行) at the end of line. */ i = strlen(Line); if (--i != 0) { if (Line[i] == '\n') Line[i] = '\0'; /* Scan the first two bytes and nb of bytes. The two bytes are read in First_Word since its use depend on the record type: if it's an extended address record or a data record. */ /* sscanf() - 从一个字符串中读进与指定格式匹配的数据, 成功则返回参数数目. ":%2x%4x%2x%s":格式说明 :冒号开头,2个十六进制数,4个十六进制数, 2个十六进制数,余下的当做字符串 */ result = sscanf (Line, ":%2x%4x%2x%s",&Nb_Bytes,&First_Word,&Type,Data_Str); if (result != 4) fprintf(stderr,"Error in line %d of hex file\n", Record_Nb); p = (char *) Data_Str; //p表示指向数据域(包括checksum)的指针 /* If we're reading the last record, ignore it. */ switch (Type) { /* Data record */ case 0: if (Nb_Bytes == 0) break; Address = First_Word; if (Seg_Lin_Select == SEGMENTED_ADDRESS) { Phys_Addr = (Segment 8) + (Segment & 0xFF) + temp2) & 0xFF; VerifyChecksumValue(); } break; /* Start segment address record */ case 3: /* Nothing to be done since it's for specifying the starting address for execution of the binary code */ break; /* Extended linear address record */ case 4: /* First_Word contains the offset. It's supposed to be 0000 so we ignore it. */ if (Address_Alignment_Word) /*默认为false*/ { sscanf (p, "%4x",&Offset); Offset = Offset 8) + (Upper_Address & 0xFF) + temp2) & 0xFF; VerifyChecksumValue(); } break; /* Start linear address record */ case 5: /* Nothing to be done since it's for specifying the starting address for execution of the binary code */ break; default: fprintf(stderr,"Unknown record type\n"); break; } } } while (!feof (Filin));

最后将数组中的内容输出到文件,即可得到bin文件。

按照上面的思路我用Java写了一个转换类(用在上面提到的ECU下载工具中),感兴趣的同学可以看一看。 其中碰到的坑有必要提一下,由于Hex文件中的数据是采用ASC II码的,而Bin文件中的数据是直接使用的二进制(不存在编码),因此在转换过程中涉及到编码转换。另外还有一点需要提一下,由于Java没有无符号型,如果将读到的数据赋给byte型变量,在调试的过程中使用print输出时会看到乱码(数据溢出byte类型的范围)。



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3