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

标签 c++ winapi

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

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/

相关文章:

c++ - 使用 SetWindowsHookEx Hook 键盘消息的问题

c++ - 使用 -g 和不使用 -DNDEBUG 选项的链接时间非常长

c++ - 64 位平台上的 WinAPI IcmpSendEcho

c++ - GetThreadContext 寄存器总是返回 0xCCCCCCCC

ruby - 如何从 Ruby 调用 CreateWindowEx?

c++ - MultiByteToWideChar 终止带有垃圾的输出缓冲区,但未报告任何错误。为什么?

c++ - 访问基类的 protected 构造函数

c++ - Visual Studio 中的 native C++ 程序

c++ - 没有 AVX2 的 32 位整数的 SSE 整数 2^n 次幂

c++ - C++ 程序员关于 Scala 的问题(结构和 STL)