c++ 解決無法打印uint8_t 類型變量的問題
將uint8_t 轉(zhuǎn)化為unsigned 類型
使用一元運(yùn)算符+(和- 運(yùn)算符對應(yīng))
測試代碼如下
#include <cstdint> #include <iostream> #include <typeinfo> 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; }
運(yùn)行結(jié)果如下
可見使用+運(yùn)算符的原理也是進(jìn)行類型轉(zhuǎn)換(把uint8_t 轉(zhuǎn)為 int)
補(bǔ)充知識:C 語言printf打印各種數(shù)據(jù)類型的方法(u8/s8/u16/s16.../u64/double/float)(全)
首先必須知道u8,s8等數(shù)據(jù)類型的定義:
typedef signed char 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;
與體系結(jié)構(gòu)相關(guān)的,定義在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;
對于各種數(shù)據(jù)類型的打印方式總結(jié)如下如下:
數(shù)據(jù)類型 | 打印格式 |
---|---|
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------>十六進(jìn)制 | %0x |
unsigned long/long---->十六進(jìn)制 | %0lx |
long long/unsigned long long ----->十六進(jìn)制 | %0llx |
unsigned int/int------>八進(jìn)制 | %0o |
unsigned long/long---->八進(jìn)制 | %0lo |
long long/unsigned long long ----->八進(jìn)制 | %0llo |
float | %f |
double | %f or %lf |
科學(xué)技術(shù)類型(必須轉(zhuǎn)化為double類型) | %e |
限制輸出字段寬度 | %x.yf (x:整數(shù)長度,y:小數(shù)點(diǎn)長度) |
待解問題,在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 < 201103L /* Defining these macros in C++98 is a GCC extension. */ #define bool bool #define false false #define true true #endif #endif /* __cplusplus */ /* Signal that all the definitions are present. */ #define __bool_true_false_are_defined 1 #endif /* stdbool.h */
也大致解釋了linux kernel bool type了。
以上這篇c++ 解決無法打印uint8_t 類型變量的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語言庫函數(shù)qsort的使用及模擬實(shí)現(xiàn)
這篇文章主要介紹了C語言庫函數(shù)qsort的使用及模擬實(shí)現(xiàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08C語言實(shí)現(xiàn)自動(dòng)給QQ好友發(fā)窗口抖動(dòng)
這篇文章主要介紹了C語言實(shí)現(xiàn)自動(dòng)給QQ好友發(fā)窗口抖動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11C++輸出斐波那契數(shù)列的兩種實(shí)現(xiàn)方法
以下是對C++中輸出斐波那契數(shù)列的兩種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10C/C++?Qt?給ListWidget組件增加右鍵菜單功能
本篇文章給大家介紹ListWidget組件增加一個(gè)右鍵菜單,當(dāng)用戶在ListWidget組件中的任意一個(gè)子項(xiàng)下右鍵,我們讓其彈出這個(gè)菜單,并根據(jù)選擇提供不同的功能,感興趣的朋友跟隨小編一起看看吧2021-11-11C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展
在C++中,定義函數(shù)時(shí)可以給形參指定一個(gè)默認(rèn)的值,這樣調(diào)用函數(shù)時(shí)如果沒有給這個(gè)形參賦值(沒有對應(yīng)的實(shí)參),那么就使用這個(gè)默認(rèn)的值。也就是說,調(diào)用函數(shù)時(shí)可以省略有默認(rèn)值的參數(shù)2022-04-04關(guān)于C++虛函數(shù)與靜態(tài)、動(dòng)態(tài)綁定的問題
這篇文章主要介紹了C++虛函數(shù)與靜態(tài)、動(dòng)態(tài)綁定,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10C++11/14 線程調(diào)用類對象和線程傳參的方法
這篇文章主要介紹了C++11/14 線程調(diào)用類對象和線程傳參的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Qt下調(diào)用vlc庫實(shí)現(xiàn)RTSP拉流播放和截圖過程詳解
這篇文章主要為大家介紹了Qt下調(diào)用vlc庫實(shí)現(xiàn)RTSP拉流播放和截圖過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Recommended C Style and Coding Standards中文翻譯版
本文翻譯自Recommended C Style and Coding Standards(C語言編碼風(fēng)格和標(biāo)準(zhǔn)),需要的朋友可以參考下2014-04-04