• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

c++ 解决无法打印uint8_t 类型变量的问题

c++ 搞代码 4年前 (2022-01-06) 20次浏览 已收录 0个评论

这篇文章主要介绍了c++ 解决无法打印uint8_t 类型变量的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

将uint8_t 转化为unsigned 类型

使用一元运算符+(和- 运算符对应)

测试代码如下

 #include  #include  #include  int main() { std::uint8_t uint8_num = 10; std::cout << "uint8_t num is " << uint8_num << std::endl; //无法打印 std::cout << "after cast to unsigned, uint8_t num is " << unsigned(uint8_num) << std::endl; //能正常打印 std::cout << "with a unary + operator, uint8_t num is " << +uint8_num << std::endl; //能正常打印 std::cout << "type of '+uint8_num' is " << typeid(+uint8_num).name() << std::endl; return 0; } 

运行结果如下

可见使用+运算符的原理也是进行类型转换(把uint8_t 转为 int)

补充知识:C 语言printf打印各种数据类型的方法(u8/s8/u16/s16…/u64/double/float)(全)

首先必须知道u8,s8等数据类型的定义:

 typedef signed cha<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码*网</strong>r s8; typedef unsigned char u8; typedef signed short s16; typedef unsigned short u16; typedef signed int s32; typedef unsigned int u32; typedef signed long long s64; typedef unsigned long long u64; 

与体系结构相关的,定义在include/linux/type.h文件中:

 /* bsd */ typedef unsigned char u_char; typedef unsigned short u_short; typedef unsigned int u_int; typedef unsigned long u_long; /* sysv */ typedef unsigned char unchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; #ifndef __BIT_TYPES_DEFINED__ #define __BIT_TYPES_DEFINED__ typedef __u8 u_int8_t; typedef __s8 int8_t; typedef __u16 u_int16_t; typedef __s16 int16_t; typedef __u32 u_int32_t; typedef __s32 int32_t; #endif /* !(__BIT_TYPES_DEFINED__) */ typedef __u8 uint8_t; typedef __u16 uint16_t; typedef __u32 uint32_t; #if defined(__GNUC__) typedef __u64 uint64_t; typedef __u64 u_int64_t; typedef __s64 int64_t; 

对于各种数据类型的打印方式总结如下如下:

数据类型 打印格式
u8 %d
s8 %d
u16 %d or %hu
s16 %d or %hd
u32 %u
s32 %d
u64 %llu
s64 %lld
int %d
unsigned int %u
short int %d or %hd
long %ld
unsigned long %lu
long long %lld
unsigned long long %llu
char %c
char * %s
bool (#define stdbool.h) %d
unsigned int/int——>十六进制 %0x
unsigned long/long—->十六进制 %0lx
long long/unsigned long long —–>十六进制 %0llx
unsigned int/int——>八进制 %0o
unsigned long/long—->八进制 %0lo
long long/unsigned long long —–>八进制 %0llo
float %f
double %f or %lf
科学技术类型(必须转化为double类型) %e
限制输出字段宽度 %x.yf (x:整数长度,y:小数点长度)

待解问题,在linux kernel里面也有使用bool来定义变量,查看code,定义如下:

typedef _Bool bool;

但是并没有真正找到具体定义在何处,待解。

下面是stdbool.h的source code:

 #define _STDBOOL_H #ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 #else /* __cplusplus */ /* Supporting _Bool in C++ is a GCC extension. */ #define _Bool bool #if __cplusplus 

也大致解释了linux kernel bool type了。

以上就是c++ 解决无法打印uint8_t 类型变量的问题的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:c++ 解决无法打印uint8_t 类型变量的问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址