您的位置:首页 > 编程语言 > C语言/C++

c++ 十六进制字符串存入文件实例

2016-12-20 17:35 281 查看
c++ 十六进制字符串存入文件:

方法一:

void writeToFile()

{

const char* str = "000000DD000000E00000015C000000E2";

FILE* file = fopen("test.txt", "wb");

assert(file);

size_t len = 0;

uint8_t* buf2 = ConvertToBinBuf(str, &len);

fwrite(buf2, 1, len, file);

free(buf2);

fclose(file);

printf(".......................\n");

}

size_t ConvertHexStrToInt(const char* hex_str, size_t length)

{
size_t sum = 0;
for (size_t i = 0; i < length; ++i)
{
int asc = (int)hex_str[i];
size_t r1 = (asc & 0x40) ? (asc & 0x0F) + 0x9 : (asc & 0x0F);
sum += (r1*pow(16, length - i - 1));
}
return sum;

}

uint8_t * ConvertToFileBuf(const char* hex_str, size_t *buf_size)

{
if (!hex_str)
{
*buf_size = 0;
return NULL;
}
//1.偶数个
size_t len = strlen(hex_str);
assert(!(len % 2));

*buf_size = len / 2;
uint8_t* buf = (uint8_t*)malloc(*buf_size);

for (size_t i = 0, j = 0; i < len; i += 2, ++j)
{
uint8_t value = (uint8_t)ConvertHexStrToInt(hex_str + i, 2);
buf[j] = value;
}
return buf;

}

方法二:

void writeToFile()

{

uint8_t buf[] =

{

0x00, 0x00, 0x00, 0xDD,

0x00, 0x00, 0x00, 0xE0,

0x00, 0x00, 0x01, 0x5C,

0x00, 0x00, 0x00, 0xE2

};

FILE* file = fopen("test.txt", "wb");

assert(file);

size_t len = 16;

fwrite(buf, 1, len, file);

fclose(file);

printf(".......................\n");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息