DIY编程器网
标题:
开源一个HEX转BIN代码
[打印本页]
作者:
hdzl
时间:
2014-1-1 10:32
标题:
开源一个HEX转BIN代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef float flt_t;
typedef double lflt_t;
#define _not_hex_(src) ((('0' > src) || ('9' < src)) && (('a' > src) || ('f' < src)) && (('A' > src) || ('F' < src)))
#define _hex_trans_eq_(src,dst) \
{ \
if (_not_hex_(src)) \
{ \
return HEX_ERR; \
} \
if (('0' <= (src)) && ('9' >= (src))) \
{ \
dst = (src) - '0'; \
} \
else if (('a' <= (src)) && ('f' >= (src))) \
{ \
dst = (src) - 'a' + 10; \
} \
else if (('A' <= (src)) && ('F' >= (src))) \
{ \
dst = (src) - 'A' + 10; \
} \
}
#define _hex_trans_or_(src,dst) \
{ \
if (_not_hex_(src)) \
{ \
return HEX_ERR; \
} \
if (('0' <= (src)) && ('9' >= (src))) \
{ \
dst |= (src) - '0'; \
} \
else if (('a' <= (src)) && ('f' >= (src))) \
{ \
dst |= (src) - 'a' + 10; \
} \
else if (('A' <= (src)) && ('F' >= (src))) \
{ \
dst |= (src) - 'A' + 10; \
} \
}
#define _hex_trans_or_shift_(src,dst) \
{ \
dst <<= 4; \
_hex_trans_or_(src,dst); \
}
enum RESULT{HEX_ERR = -1, HEX_DATA_OK = 0, HEX_END_OK = 1, HEX_LBA_OK = 2, HEX_SEG_OK = 3};
int8_t _hex2bin_(const uint8_t* src, uint8_t* dst, uint8_t* data_type, uint8_t* data_len, uint16_t* data_addr)
{
uint8_t check_sum, i;
// 如果源code长度小于11,表明源code有误!
if (11 > strlen((const char*)src))
{
return HEX_ERR;
}
// 校验起始字节是否为':',如果不为':',表明源code有误!
if (':' != *src++)
{
return HEX_ERR;
}
// 得到数据长度的高4位
_hex_trans_eq_(*src, *data_len);
src++;
// 得到数据长度的低4位
_hex_trans_or_shift_(*src, *data_len);
src++;
// 校验和
check_sum = *data_len;
// 得到数据地址
_hex_trans_eq_(*src, *data_addr);
src++;
_hex_trans_or_shift_(*src, *data_addr);
src++;
// 校验和
check_sum += *data_addr;
_hex_trans_or_shift_(*src, *data_addr);
src++;
_hex_trans_or_shift_(*src, *data_addr);
src++;
// 校验和
check_sum += *data_addr;
// 得到数据类型
_hex_trans_eq_(*src, *data_type);
src++;
_hex_trans_or_shift_(*src, *data_type);
src++;
// 校验和
check_sum += *data_type;
switch (*data_type)
{
case 0: // 数据记录
for (i = *data_len; i; i--, dst++)
{
_hex_trans_eq_(*src, *dst);
src++;
_hex_trans_or_shift_(*src, *dst);
src++;
// 校验和
check_sum += *dst;
}
// 校验
_hex_trans_eq_(*src, i);
src++;
_hex_trans_or_shift_(*src, i);
check_sum += i;
return check_sum? HEX_ERR : HEX_DATA_OK;
case 1: // 文件结束记录
if (*data_addr)
{
return HEX_ERR;
}
// 校验
_hex_trans_eq_(*src, i);
src++;
_hex_trans_or_shift_(*src, i);
check_sum += i;
return check_sum? HEX_ERR : HEX_END_OK;
case 2: // 扩展段地址记录
if (*data_addr)
{
return HEX_ERR;
}
// 得到扩展段地址
_hex_trans_eq_(*src, *data_addr);
src++;
_hex_trans_or_shift_(*src, *data_addr);
src++;
// 校验和
check_sum += *data_addr;
_hex_trans_or_shift_(*src, *data_addr);
src++;
_hex_trans_or_shift_(*src, *data_addr);
src++;
// 校验和
check_sum += *data_addr;
return check_sum? HEX_ERR : HEX_SEG_OK;
case 4: // 扩展线性地址记录
if (*data_addr)
{
return HEX_ERR;
}
// 得到扩展段地址
// 得到数据地址
_hex_trans_eq_(*src, *data_addr);
src++;
_hex_trans_or_shift_(*src, *data_addr);
src++;
// 校验和
check_sum += *data_addr;
_hex_trans_or_shift_(*src, *data_addr);
src++;
_hex_trans_or_shift_(*src, *data_addr);
src++;
// 校验和
check_sum += *data_addr;
return check_sum? HEX_ERR : HEX_LBA_OK;
default:
return HEX_ERR;
}
return HEX_ERR;
}
int8_t hex2bin(const int8_t* src_file_path, const int8_t* dst_file_path)
{
uint8_t buffer_hex[1024], buffer_bin[256];
uint8_t data_type, len_bin;
uint16_t addr_low;
uint32_t addr_high = 0;
FILE* src_file;
FILE* dst_file;
src_file = fopen((const char*)src_file_path, "r");
if (!src_file)
{
return -1;
}
dst_file = fopen((const char*)dst_file_path, "wb");
if (!dst_file)
{
fclose(src_file);
return -1;
}
for ( ; !feof(src_file); )
{
if (NULL == fgets((char*)buffer_hex, 1024, src_file))
{
break;
}
if (HEX_ERR == _hex2bin_((const uint8_t*)buffer_hex, (uint8_t*)buffer_bin, &data_type, &len_bin, &addr_low))
{
break;
}
switch (data_type)
{
case 0: // 数据记录
if (ftell(dst_file) != addr_low + addr_high)
{
fseek(dst_file, addr_low + addr_high, SEEK_SET);
}
if (1 != fwrite((const uint8_t*)buffer_bin, len_bin, 1, dst_file))
{
fclose(src_file);
fclose(dst_file);
return -1;
}
break;
case 1: // 文件结束记录
fclose(src_file);
fclose(dst_file);
return 0;
case 2: // 扩展段地址记录
addr_high = ((uint32_t)addr_low) << 2;
break;
case 4: // 扩展线性地址记录
addr_high = ((uint32_t)addr_low) << 16;
break;
default:
fclose(src_file);
fclose(dst_file);
return -1;
}
}
fclose(src_file);
fclose(dst_file);
return 0;
}
int main(void)
{
if (!hex2bin((const int8_t*)"avr-test.hex",(const int8_t*)"avr-test.bin"))
printf("ok!\n");
return 0;
}
欢迎光临 DIY编程器网 (http://diybcq.com/)
Powered by Discuz! X3.2