我正在使用以下代码片段将整数转换为 vector<unsigned char>
:
const vector<unsigned char> int_to_bytearray(const size_t value) noexcept {
unsigned char bytes[4];
bytes[0] = (value >> 24) & 0xFF;
bytes[1] = (value >> 16) & 0xFF;
bytes[2] = (value >> 8) & 0xFF;
bytes[3] = value & 0xFF;
const vector<unsigned char>to_return(bytes, bytes + sizeof bytes);
return to_return;
}
如果我传递值 196608
我得到 {'\x00', '\x03', '\x00', '\x00'}
这是我所期望的。如果我通过 37
我得到 { 0, 0, 0, % }
百分比标记让我失望。我错过了什么?
最佳答案
您得到的是“%”字符,其代码为 37。所以一切都按预期工作。
关于c++ - 将整数转换为字节数组时获取奇数字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027956/