在C ++中使用std :: cout打印uint8_t变量 [英] Printing uint8_t variables using std::cout in C++

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

问题描述

在以下代码中,似乎 std :: cout 不能正确打印uint8_t类型的变量.

In the following code, it appears that std::cout does not print the variable of type uint8_t properly.

int main() {
    uint8_t var = 16;
    std::cout << "value: " << var << std::endl;
}

输出:

value: 

类似的uintX_t类型我没有问题.

I don't have problem with similar uintX_t types.

我知道我在这里遗漏了一些东西,但是我不知道它是什么.

I know I'm missing something here, but I can't figure what it is.

推荐答案

uint8_t 在大多数系统上都映射到 unsigned char .结果,调用将 16 解释为不可打印字符的替代.将强制类型转换添加到 int 以查看数值:

uint8_t maps to unsigned char on most systems. As the result, the override that interprets 16 as a non-printable character gets invoked. Add a cast to int to see the numeric value:

std::cout << "value: " << (int)var << std::endl;

这篇关于在C ++中使用std :: cout打印uint8_t变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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