读写16进制数文件
来源:互联网 发布:华为荣耀4xroot软件 编辑:程序博客网 时间:2024/04/27 10:14
将16进制数据按照以下格式读入缓存或者写入到文件:
55 AA 01 85 00 00 8C 31 20 20 33 30 30 30 30 30 2E 31 35 53 6F 6C 61 72 52 69 76 65 72 2D 55 53 20 20 20 53 61 6D 69 6C 50 6F 77 65 72 20 20 20 20 20 20 57 58 30 30 30 30 30 30 30 31 00 00 00 00 00 00 30 32 2E 30 34 30 30 2E 31 35 32 00 01 F4 01 0A 14 02 08 84 04 00 61 05 00 A3 07 00 00 08 00 00 09 00 00 0A 00 00 0C 00 00 27 03 E8 28 03 E8 31 01 F4 32 08 98 33 01 F4 34 15 FF 11 00 00 17 00 00 18 00 00 19 00 00 1A 00 00 1B 00 00 1C 00 00 1C 98
读:
void transHexNum(char buf[], int index, int cHigh, int cLow)
{
//LOG_DEBUG("cHigh = %c, cLow = %c", cHigh, cLow);
if (cHigh >= '0' && cHigh <= '9')
{
buf[index] |= ((cHigh - '0') & 0xf) << 4;
}
else
{
switch (cHigh)
{
case 'a':
case 'A':
buf[index] |= 0xa0;
break;
case 'b':
case 'B':
buf[index] |= 0xb0;
break;
case 'c':
case 'C':
buf[index] |= 0xc0;
break;
case 'd':
case 'D':
buf[index] |= 0xd0;
break;
case 'e':
case 'E':
buf[index] |= 0xe0;
break;
case 'f':
case 'F':
buf[index] |= 0xf0;
break;
default:
break;
}
}
if (cLow >= '0' && cLow <= '9')
{
buf[index] |= (cLow - '0') & 0xf;
}
else
{
switch (cLow)
{
case 'a':
case 'A':
buf[index] |= 0xa;
break;
case 'b':
case 'B':
buf[index] |= 0xb;
break;
case 'c':
case 'C':
buf[index] |= 0xc;
break;
case 'd':
case 'D':
buf[index] |= 0xd;
break;
case 'e':
case 'E':
buf[index] |= 0xe;
break;
case 'f':
case 'F':
buf[index] |= 0xf;
break;
default:
break;
}
}
}
void ReadHexFile()
{
char *file = "/home/XXX/hex";
int fd = open(file, O_RDONLY);
if (-1 == fd)
{
LOG_ERROR("open %s failed !", file);
return;
}
ssize_t size;
char hexValue[449] = {0};
int i, j;
char srdData[149] = {0};
while ((size = read(fd, hexValue, sizeof(char) * 449)) > 0)
{
LOG_WARNING("---------------------------------");
memset(srdData, 0, sizeof(srdData));
for (i = 0, j = 0; j < 149; i += 3, j++)
{
if (j < 149)
{
transHexNum(srdData, j, hexValue[i], hexValue[i+1]);
LOG_INFO("srdData[%d] = 0x%x", j, srdData[j]);
}
}
memset(hexValue, 0, sizeof(hexValue));
}
close(fd);
}
写:
void printArray(char buf[], int size, FILE *file)
{
int i;
for (i = 0; i < size; i++)
{
fprintf(file, "0x%02X ", buf[i]);
}
fprintf(file, "\n\n");
}
void WriteHexFile()
{
char value[149] = {0x55, 0xAA, 0x01, 0x85, 0x00, 0x00, 0x8C, 0x31, 0x20, 0x20, 0x33, 0x30,
0x30, 0x30, 0x30, 0x30, 0x2E, 0x31, 0x35, 0x53, 0x6F, 0x6C, 0x61, 0x72,
0x52, 0x69, 0x76, 0x65, 0x72, 0x2D, 0x55, 0x53, 0x20, 0x20, 0x20, 0x53,
0x61, 0x6D, 0x69, 0x6C, 0x50, 0x6F, 0x77, 0x65, 0x72, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x57, 0x58, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x32, 0x2E, 0x30, 0x34,
0x30, 0x30, 0x2E, 0x31, 0x35, 0x32, 0x00, 0x01, 0xF4, 0x01, 0x0A, 0x14,
0x02, 0x08, 0x84, 0x04, 0x00, 0x61, 0x05, 0x00, 0xA3, 0x07, 0x00, 0x00,
0x08, 0x00, 0x00, 0x09, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x0C, 0x00, 0x00,
0x27, 0x03, 0xE8, 0x28, 0x03, 0xE8, 0x31, 0x01, 0xF4, 0x32, 0x08, 0x98,
0x33, 0x01, 0xF4, 0x34, 0x15, 0xFF, 0x11, 0x00, 0x00, 0x17, 0x00, 0x00,
0x18, 0x00, 0x00, 0x19, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x1B, 0x00, 0x00,
0x1C, 0x00, 0x00, 0x1C, 0x98};
//LOG_INFO("length = %d", (int)(sizeof(value) / sizeof(value[0])));
FILE *file;
file = fopen("/home/XXX/hex", "w"); // 如果是追加写,参数改成 "a"
setbuf(file, NULL); // 设成无缓冲 IO
printArray(value, 149, file);
fflush(file); // 强迫将缓存区中的数据写到指定的文件中
fclose(file);
}
- 读写16进制数文件
- Exercise 16: 读写文件
- 练习16:文件读写
- C++16、文件读写
- 习题16 读写文件
- 笨办法16读写文件
- 文件读写、剪贴板读写
- 读写文件
- 文件读写
- 读写文件
- 读写文件
- 文件读写
- 读写文件
- 读写文件
- 读写文件
- 文件读写
- 文件读写
- 文件读写
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- eclipse中UTF-8设置
- VIM括号自动补全
- js获取loacation中参数的方法
- Android开发常识
- 读写16进制数文件
- vs 2010 sp1
- 重构理论及实践——用工厂模式重构c++后台代码
- MySQL入门介绍
- sql优化原则
- UISearchBar自定义右侧取消按钮
- 线程优先级设置
- 常用js代码片段
- 创业分享:要选择一个问题去解决,而不是选择一个点子去创业