我有一个关于在c++中处理整数的奇怪问题。

我写了一个简单的程序,将一个值设置为一个变量,然后打印它,但它不像预期的那样工作。

我的程序只有两行代码:

uint8_t aa = 5;

cout << "value is " << aa << endl;

这个程序的输出是value is

也就是说,它为aa打印空白。

当我把uint8_t改为uint16_t时,上面的代码就像一个魅力。

我使用Ubuntu 12.04 (Precise Pangolin), 64位,我的编译器版本是:

gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

当前回答

cout将aa作为ASCII值5的char,这是一个不可打印的字符,在打印之前尝试类型转换为int。

其他回答

这是因为输出操作符把uint8_t当作字符(uint8_t通常只是unsigned char的别名),所以它用ASCII码打印字符(这是最常见的字符编码系统)5。

参见例如这篇参考文献。

cout将aa作为ASCII值5的char,这是一个不可打印的字符,在打印之前尝试类型转换为int。

在任何基元数据类型的变量之前添加一元+运算符将给出可打印的数值而不是ASCII字符(在char类型的情况下)。

uint8_t aa = 5;
cout<<"value is "<< +aa <<endl; // value is 5

Making use of ADL (Argument-dependent name lookup): #include <cstdint> #include <iostream> #include <typeinfo> namespace numerical_chars { inline std::ostream &operator<<(std::ostream &os, char c) { return std::is_signed<char>::value ? os << static_cast<int>(c) : os << static_cast<unsigned int>(c); } inline std::ostream &operator<<(std::ostream &os, signed char c) { return os << static_cast<int>(c); } inline std::ostream &operator<<(std::ostream &os, unsigned char c) { return os << static_cast<unsigned int>(c); } } int main() { using namespace std; uint8_t i = 42; { cout << i << endl; } { using namespace numerical_chars; cout << i << endl; } } output: * 42 A custom stream manipulator would also be possible. The unary plus operator is a neat idiom too (cout << +i << endl).

std::ostream和char之间的操作符<<()重载是非成员函数。您可以显式地使用成员函数将char(或uint8_t)作为int处理。

#include <iostream>
#include <cstddef>

int main()
{
   uint8_t aa=5;

   std::cout << "value is ";
   std::cout.operator<<(aa);
   std::cout << std::endl;

   return 0;
}

输出:

value is 5