简体   繁体   English

用于将十六进制字符串转换为字符数组的单行

[英]one-liner for convert hex string to char array

This is probably a duplicate of hex string to unsigned char but with different input这可能是十六进制字符串到无符号字符的重复,但输入不同

I want to read hex strings from txt file and tried to save it in array我想从 txt 文件中读取十六进制字符串并尝试将其保存在数组中

FF FF FF FF FF FF FF FF
0 3F 0 0 3 E0 0 1
7F 0 43 1 92 FF 0 AA

Expected output:预期输出:

unsigned char array[3][8] = {
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0x0, 0x3F, 0x0, 0x0, 0x3, 0xE0, 0x0, 0x1,
  0x7F, 0x0, 0x43, 0x1, 0x92, 0xFF, 0x0, 0xAA
};

Note: Line ending is important.注意:行尾很重要。 Some lines representing less than 8 bytes一些行表示少于 8 个字节

I could show you 50 lines of code but that is completely garbage.我可以给你看 50 行代码,但这完全是垃圾。 My question isn't there any one-liner std::something to achieve this?我的问题是没有任何单行 std::something 来实现这一目标吗?

You can stream it:您可以流式传输它:

std::ifstream file(…);
std::vector<char> buf;
while(file)
{
    int c;
    file >> std::hex >> c;
    buf.push_back(c);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM