你好,我正在做一个项目。获取无符号的16位数据并将其平均。获得平均值没有问题,但是当我尝试打印屏幕时,它会打印无意义的符号。因此,ı找出ı必须转换为十进制。 Vs2015转换它,但我想自己做,因为我的代码用于微处理器。这是我的模拟...
int main(){
uint32_t x = 0x12345678;
unsigned char c[4];
c[0] = x >> 24;// least significant
c[1] = x >> 16;
c[2] = x >> 8;
c[3] = x; // most significant
cout << x << endl;
cout << c[0] << endl;
cout << c[1] << endl;
cout << c[2] << endl;
cout << c[3] << endl;
system("pause");
return 0;
}
输出:
305419896
4
V
x
这里的问题是,插入运算符<<
会将char
变量视为字符而不是数字。因此,如果char
变量包含65,它将不会打印65,而是显示'A'。
您需要将值转换为int
。
所以:
std::cout << static_cast<int>(c[0]) << "\n";
然后它将为您提供预期的输出。