uint8_t不能用cout打印 [英] uint8_t can't be printed with cout

查看:1048
本文介绍了uint8_t不能用cout打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,使用整数在c ++。

I have a weird problem about working with integers in c++.

我写了一个简单的程序,设置一个值到一个变量,然后打印它,不正常工作。

I wrote a simple program that sets a value to a variable and then prints it, but it is not working as expected.

我的程序只有两行代码:

My program has only two lines of code:

uint8_t aa=5;

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

此程序的输出为 value is

ie当我更改 uint8_t aa 输出空格。

i.e. it prints blank for aa.

> uint16_t 上面的代码就像一个charm。

when i change uint8_t to uint16_t the above code works like a charm.

我使用Ubuntu 12.04(64位)版本是: gcc版本4.6.3(Ubuntu / Linaro 4.6.3-1ubuntu5)

I use Ubuntu 12.04 (64 bit) and my compiler version is :gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

推荐答案

它不会真正打印一个空白,但最可能的ASCII字符值为5,这是不可见的。有许多不可见的ASCII字符代码,其中大多数都低于值32,这是

It doesn't really print a blank, but most probably the ASCII character with value 5, which is invisible. There's a number of invisible ASCII character codes, most of them below value 32, which is the blank actually.

您必须将 aa 转换为 unsigned int 输出数值,因为 ostream&

You have to convert aa to unsigned int to output the numeric value, since ostream& operator<<(ostream&, unsigned char) tries to output the visible character value.

uint8_t aa=5;

cout << "value is " << unsigned(aa) << endl;

这篇关于uint8_t不能用cout打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆