gpt4 book ai didi

C++ 从字符串化字节转换字节数组

转载 作者:行者123 更新时间:2023-11-28 02:52:52 32 4
gpt4 key购买 nike

我有一个非常大的字符串,如下所示:

std::string tmpString = "0xC7,0x04,0x33,0xC0,0x49,0x74,0x0A,..."

我想将每个字符串化的字节转换回一个字节数组。这种情况的最佳解决方案是什么?

我正在使用 VC++

最佳答案

试试这个:

std::string tmpString = "0xC7,0x04,0x33,0xC0,0x49,0x74,0x0A,...";
...
std::vector<BYTE> bytes;
bytes.reserve((tmpString.length() / 5) + 1);
std::istringstream iss(tmpString);
std::string s;
while (std::getline(iss, s, ',')) {
WORD num; // istreamstream does not have an '>>' operator for bytes
std::istringstream(s) >> std::hex >> num;
bytes.push_back(BYTE(num));
}

关于C++ 从字符串化字节转换字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22648711/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com