【发布时间】:2017-08-09 18:59:04
【问题描述】:
接下来,我将二进制数字 15 编码为 unsigned char,然后将其转换为 int。但是,当我用 int8_t 替换 int 时,它不再输出 15。我不明白为什么。 int8_t的大小不是8位吗,所以应该和char匹配得很好,也是8位?
#include <iostream>
#include <string.h>
int main()
{
unsigned char binValue = 0<<7 | 0<<6 | 0<<5 | 0<<4 | 1<<3 | 1<<2 | 1<<1 | 1<<0; // this is 15 in binary
int intValue = (int)binValue;
// memcpy(&intValue,&binValue,sizeof(int)); // Or this one
std::cout << intValue << std::endl;
return 0;
}
【问题讨论】:
-
int8_t已签名。试试uint8_t。 “停止工作”是什么意思? -
顺便说一句,这看起来很像 C++
-
@Jean-FrançoisFabre 对不起。我的意思是输出不再是预期的 15。程序仍然编译并运行。