c++ - 将整数转换为字节数组时获取奇数字符

标签 c++ c++11

我正在使用以下代码片段将整数转换为 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/

相关文章:

c++ - 为什么在列表初始化函数参数时显式构造函数不匹配?

c++ - 我类的析构函数做错了吗? C++

c++ - 如何找到该图像或多或少是均匀的 w.r.t 颜色(色调)?

c++ - Swift 中的神经网络

c++ - "<<"运算符不适用于 visual studio 2012 中的字符串数组

c++ - -std=c++11 是否适合使用 gcc 4.8.1 的生产代码

c++ - 如何让列表可以隐式转换为我的类对象?

c++ - 动态数组分配 C++

c++ - 将 std::atomic_flag 与工作线程一起使用时出现问题

c++ - 从 C++98 升级到 C++11 导致错误