我想在C++中使用无符号的8位变量.关于算术的任何一个unsigned char或者uint8_t做的伎俩(这是预期的,因为AFAIK uint8_t只是一个别名unsigned char,或者调试器提出它.
问题是,如果我在C++中使用ostream打印出变量,它会将其视为char.如果我有:
unsigned char a = 0;
unsigned char b = 0xff;
cout << "a is " << hex << a <<"; b is " << hex << b << endl;
Run Code Online (Sandbox Code Playgroud)
然后输出是:
a is ^@; b is 377
Run Code Online (Sandbox Code Playgroud)
代替
a is 0; b is ff
Run Code Online (Sandbox Code Playgroud)
我尝试过使用uint8_t,但正如我之前提到的,那是typedef'ed unsigned char,所以它也是如此.如何正确打印变量?
编辑:我在我的代码中的许多地方都这样做.有没有什么办法可以做到这一点,而不铸造int我想打印每一次?
由于在不存在限定符char时,C++中的a的性质是编译器相关的unsigned,是否有一个参数可以传递给GCC,这会强制所有的chars被编译为unsigned?