如何从位数组变为字节?
How can I go from a bit array to an byte?
我有这个位数组
int bits[8] = { 0, 1, 1, 0, 0, 1, 0, 1 }
十六进制为 65,十进制为 101。ASCII 字母是"e"。如何将数组读取为 char 和 int(十进制值(?
您可以使用位移位来从位数组中获取字符,如下所示:
int bits[8] = { 0, 1, 1, 0, 0, 1, 0, 1 };
char result = 0; // store the result
for(int i = 0; i < 8; i++){
result += (bits[i] << (7 - i)); // Add the bit shifted value
}
cout << result;
这基本上是遍历数组,按正确的量进行位移,然后将值添加到聚合的"结果"变量中。输出应为"e"。
相关文章:
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- C++字节数组到整数
- 将字符串转换为无符号字符数组/字节数组
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- 从 UTF-8 字节数组创建字符串?
- 如何将 CString 转换为字节 [] 数组?
- 使用 ESP8266/8285 将字符串从 Web 服务器转换为字节数组
- 如何将 (serverClients[i].read() 传递给 ESP8266 上的字节数组
- C++浮点数和字节数组的联合问题
- 如何初始化字节数组的映射?