日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于uint8_t/uint16_t/uint32_t/uint_fast16_t

發(fā)布時間:2024/4/24 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于uint8_t/uint16_t/uint32_t/uint_fast16_t 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


在嵌入式編程中經(jīng)常遇到用uint8_t、uint16_t、uint32_t、uint_fast16_t之類的關鍵字定義一些整型變量,但是具體表示什么意思,并不是太清楚,只是把它當成int之類的整型變量定義關鍵字。在自己理解他們之前,先寫一下在網(wǎng)上搜到的常見的對他們的解釋。

常見解釋(都是個人見解,不一定準確全面)

#define uint unsigned int; int和uint的區(qū)別:一個有符號,一個無符號。 uint在單片機中經(jīng)常用到,定義一個無符號整型變量。

論壇上就有人問:以*_t結尾的類型是不是都是long型的?在baidu上查一下,才找到答案,這時才發(fā)覺原來自己對C掌握的太少。

那么_t的意思到底表示什么?具體的官方答案沒有找到,不過我覺得有個答案比較接近。它就是一個結構的標注,可以理解為type/typedef的縮寫,表示它是通過typedef定義的,而不是其它數(shù)據(jù)類型。

uint8_t,uint16_t,uint32_t等都不是什么新的數(shù)據(jù)類型,它們只是使用typedef給類型起的別名,新瓶裝老酒的把戲。不過,不要小看了typedef,它對于你代碼的維護會有很好的作用。比如C中沒有bool,于是在一個軟件中,一些程序員使用int,一些程序員使用short,會比較混亂,最好就是用一個typedef來定義,如:
typedef char bool;

一般來說,一個C的工程中一定要做一些這方面的工作,因為你會涉及到跨平臺,不同的平臺會有不同的字長,所以利用預編譯和typedef可以讓你最有效的維護你的代碼。為了用戶的方便,C99標準的C語言硬件為我們定義了這些類型,我們放心使用就可以了。

?按照posix標準,一般整形對應的*_t類型為:
1字節(jié)?? ? uint8_t
2字節(jié)?? ? uint16_t
4字節(jié)?? ? uint32_t
8字節(jié)?? ? uint64_t

C語言中好像沒有這種數(shù)據(jù)類型,但是在實際應用的過程中,發(fā)現(xiàn)許多人的代碼中都存在這種表示方式。其實uintX-t就是通過typedef定義的,利用預編譯和typedef可提高效率也方便代碼移植。總結如下:

typedef unsigned char???uint8_t;?????//無符號8位數(shù)

????typedef signed???char???int8_t;??????//有符號8位數(shù)

????typedef unsigned int????uint16_t;????//無符號16位數(shù)

????typedef signed???int????int16_t;?????//有符號16位數(shù)

????typedef unsigned long???uint32_t;????//無符號32位數(shù)

????typedef signed???long???int32_t;?????//有符號32位數(shù)

????typedef float???????????float32;?????//單精度浮點數(shù)

typedef double??????????float64;?????//雙精度浮點數(shù)

一般來說整形對應的*_t類型為:
??? uint8_t1字節(jié)????

??? uint16_t為2字節(jié)??

??? uint32_t為4字節(jié)????

??? uint64_t為8字節(jié)????

不難看出,通過頭文件X.h定義了uint8_t,其實編譯器實際上是把它作為"char"來處理的,在對字符型的變量進行操作。以上僅做參考,有錯誤請指出。



uint8_t / uint16_t / uint32_t /uint64_t 是什么數(shù)據(jù)類型

這些數(shù)據(jù)類型是 C99 中定義的,具體定義在:/usr/include/stdint.h ? ?ISO C99: 7.18 Integer types <stdint.h>

[objc] view plain copy print?
  • /*?There?is?some?amount?of?overlap?with?<sys/types.h>?as?known?by?inet?code?*/??
  • #ifndef?__int8_t_defined??
  • #?define?__int8_t_defined??
  • typedef?signed?char?????????????int8_t;???
  • typedef?short?int???????????????int16_t;??
  • typedef?int?????????????????????int32_t;??
  • #?if?__WORDSIZE?==?64??
  • typedef?long?int????????????????int64_t;??
  • #?else??
  • __extension__??
  • typedef?long?long?int???????????int64_t;??
  • #?endif??
  • #endif??
  • ??
  • /*?Unsigned.??*/??
  • typedef?unsigned?char???????????uint8_t;??
  • typedef?unsigned?short?int??????uint16_t;??
  • #ifndef?__uint32_t_defined??
  • typedef?unsigned?int????????????uint32_t;??
  • #?define?__uint32_t_defined??
  • #endif??
  • #if?__WORDSIZE?==?64??
  • typedef?unsigned?long?int???????uint64_t;??
  • #else??
  • __extension__??
  • typedef?unsigned?long?long?int??uint64_t;??
  • #endif??
  • /* There is some amount of overlap with <sys/types.h> as known by inet code */ #ifndef __int8_t_defined # define __int8_t_defined typedef signed char int8_t; typedef short int int16_t; typedef int int32_t; # if __WORDSIZE == 64 typedef long int int64_t; # else __extension__ typedef long long int int64_t; # endif #endif/* Unsigned. */ typedef unsigned char uint8_t; typedef unsigned short int uint16_t; #ifndef __uint32_t_defined typedef unsigned int uint32_t; # define __uint32_t_defined #endif #if __WORDSIZE == 64 typedef unsigned long int uint64_t; #else __extension__ typedef unsigned long long int uint64_t; #endif

    格式化輸出:

    unit64_t ? ??%llu ??

    unit32_t ? ? %u

    unit16_t ? ?%hu

    注意:

    必須小心 uint8_t 類型變量的輸出,例如如下代碼,會輸出什么呢?

    uint8_t fieldID = 67;
    cerr<< "field=" << fieldID <<endl;

    結果發(fā)現(xiàn)是:field=C 而 不是我們所想的?field=67

    這是由于 typedef?unsigned?char?uint8_t;?

    uint8_t 實際是一個 char,?cerr << 會輸出 ASCII 碼是 67 的字符,而不是 67 這個數(shù)字.

    因此,輸出 uint8_t 類型的變量實際輸出的是其對應的字符, 而不是真實數(shù)字.

    若要輸出 67,則可以這樣:

    cerr<< "field=" << (uint16_t)?fieldID <<endl;

    結果是:field=67

    同樣:?uint8_t 類型變量轉(zhuǎn)化為字符串以及字符串轉(zhuǎn)化為?uint8_t 類型變量都要注意,?uint8_t類型變量轉(zhuǎn)化為字符串時得到的會是ASCII碼對應的字符, 字符串轉(zhuǎn)化為 uint8_t 變量時, 會將字符串的第一個字符賦值給變量.

    例如如下代碼:

    [html] view plain copy print?
  • #include?<iostream>??
  • #include?<stdint.h>??
  • #include?<sstream>??
  • using?namespace?std;??
  • ??
  • ??
  • int?main()??
  • {??
  • ????uint8_t?fieldID?=?67;??
  • ??
  • ????//?uint8_t?-->?string??
  • ????string?s;??
  • ????ostringstream?strOStream;??
  • ????strOStream?<<?fieldID;??
  • ????s?=?strOStream.str();??
  • ????cerr?<<?s?<<?endl;??
  • ??????
  • ????//?string?-->?uint8_t??
  • ????s?=?"65";???
  • ????stringstream?strStream;??
  • ????strStream?<<?s;??
  • ????strStream?>>?fieldID;??
  • ????strStream.clear();??
  • ????cerr?<<?fieldID?<<?endl;??
  • }??
  • #include <iostream> #include <stdint.h> #include <sstream> using namespace std;int main() {uint8_t fieldID = 67;// uint8_t --> stringstring s;ostringstream strOStream;strOStream << fieldID;s = strOStream.str();cerr << s << endl;// string --> uint8_ts = "65"; stringstream strStream;strStream << s;strStream >> fieldID;strStream.clear();cerr << fieldID << endl; }

    上述代碼輸出的是:

    C

    6

    自己理解

    一下是CodeBlock編譯環(huán)境下stdint.h頭文件中關于uint8_t等的一些定義typedef命名。
    [html] view plain copy print?
  • /*?7.18.1.1??Exact-width?integer?types?*/??
  • typedef?signed?char?int8_t;??
  • typedef?unsigned?char???uint8_t;??
  • typedef?short??int16_t;??
  • typedef?unsigned?short??uint16_t;??
  • typedef?int??int32_t;??
  • typedef?unsigned???uint32_t;??
  • __MINGW_EXTENSION?typedef?long?long??int64_t;??
  • __MINGW_EXTENSION?typedef?unsigned?long?long???uint64_t;??
  • ??
  • /*?7.18.1.2??Minimum-width?integer?types?*/??
  • typedef?signed?char?int_least8_t;??
  • typedef?unsigned?char???uint_least8_t;??
  • typedef?short??int_least16_t;??
  • typedef?unsigned?short??uint_least16_t;??
  • typedef?int??int_least32_t;??
  • typedef?unsigned???uint_least32_t;??
  • __MINGW_EXTENSION?typedef?long?long??int_least64_t;??
  • __MINGW_EXTENSION?typedef?unsigned?long?long???uint_least64_t;??
  • ??
  • /*??7.18.1.3??Fastest?minimum-width?integer?types??
  • ?*??Not?actually?guaranteed?to?be?fastest?for?all?purposes??
  • ?*??Here?we?use?the?exact-width?types?for?8?and?16-bit?ints.??
  • ?*/??
  • typedef?signed?char?int_fast8_t;??
  • typedef?unsigned?char?uint_fast8_t;??
  • typedef?short??int_fast16_t;??
  • typedef?unsigned?short??uint_fast16_t;??
  • typedef?int??int_fast32_t;??
  • typedef?unsigned??int??uint_fast32_t;??
  • __MINGW_EXTENSION?typedef?long?long??int_fast64_t;??
  • __MINGW_EXTENSION?typedef?unsigned?long?long???uint_fast64_t;??
  • ??
  • /*?7.18.1.5??Greatest-width?integer?types?*/??
  • __MINGW_EXTENSION?typedef?long?long??intmax_t;??
  • __MINGW_EXTENSION?typedef?unsigned?long?long???uintmax_t;??
  • /* 7.18.1.1 Exact-width integer types */ typedef signed char int8_t; typedef unsigned char uint8_t; typedef short int16_t; typedef unsigned short uint16_t; typedef int int32_t; typedef unsigned uint32_t; __MINGW_EXTENSION typedef long long int64_t; __MINGW_EXTENSION typedef unsigned long long uint64_t;/* 7.18.1.2 Minimum-width integer types */ typedef signed char int_least8_t; typedef unsigned char uint_least8_t; typedef short int_least16_t; typedef unsigned short uint_least16_t; typedef int int_least32_t; typedef unsigned uint_least32_t; __MINGW_EXTENSION typedef long long int_least64_t; __MINGW_EXTENSION typedef unsigned long long uint_least64_t;/* 7.18.1.3 Fastest minimum-width integer types* Not actually guaranteed to be fastest for all purposes* Here we use the exact-width types for 8 and 16-bit ints.*/ typedef signed char int_fast8_t; typedef unsigned char uint_fast8_t; typedef short int_fast16_t; typedef unsigned short uint_fast16_t; typedef int int_fast32_t; typedef unsigned int uint_fast32_t; __MINGW_EXTENSION typedef long long int_fast64_t; __MINGW_EXTENSION typedef unsigned long long uint_fast64_t;/* 7.18.1.5 Greatest-width integer types */ __MINGW_EXTENSION typedef long long intmax_t; __MINGW_EXTENSION typedef unsigned long long uintmax_t;


    總結

    以上是生活随笔為你收集整理的关于uint8_t/uint16_t/uint32_t/uint_fast16_t的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 嫩草社区| 人妻互换一二三区激情视频 | 国产视频一区二区在线播放 | 亚洲精品视频观看 | 天狂传说之巴啦啦小魔仙 | 99riAv国产精品无码鲁大师 | 电影一区二区三区 | 久久久久人妻一区 | 亚洲天堂五月 | 久操免费视频 | 国产91在线看 | 男人天堂2020 | 伊人中文字幕在线观看 | 奇米一区二区三区 | 国产一二三区精品 | 久久理论视频 | zzjizzji亚洲日本少妇 | 天天色综合av | 2019最新中文字幕 | 综合久久国产 | 久久乐av | 裸体黄色片| 天天槽 | 亚洲综合色成人 | 亚洲国产精品欧美久久 | 欧美一级专区免费大片 | 国产精品久久久一区二区 | 青青草视频在线观看 | 新版红楼梦在线高清免费观看 | 欧美色欧美色 | 精品国产欧美日韩 | av日韩精品 | 国产精选第一页 | 岛国午夜视频 | 青娱乐97 | 久久黑人 | 日本中文字幕在线免费观看 | 美女羞羞动态图 | 露脸啪啪清纯大学生美女 | 熟女自拍一区 | 国产日韩欧美不卡 | 欧美在线免费观看 | 四色成人av永久网址 | 内射无码专区久久亚洲 | 久久在现| 欧美亚洲另类小说 | 亚洲小视频网站 | 玖玖网| 亚洲男人网| 依人在线 | 亚洲精品免费在线播放 | 国产视频一二三区 | 中文字幕在线视频播放 | 亚洲欧美成人 | 妺妺窝人体色www在线小说 | 7x7x7x人成影视 | 天堂成人在线观看 | 亚洲最新av | 人人爱人人澡 | 另类老妇性bbwbbw图片 | www天天操 | 久久久精品国产sm调教网站 | av片一区二区| 久久久久久天堂 | 捅肌肌 | 国产视频久久久久久久 | 免费看黄色的网址 | 欧美日韩精品一区二区三区蜜桃 | 免费一级特黄特色毛片久久看 | 欧美大片一区二区三区 | 国产成人av无码精品 | jizz日本在线播放 | 91avcom | 特黄一级片 | 亚洲天堂网在线观看视频 | 午夜88 | 顶级尤物极品女神福利视频 | 色婷婷综合成人av | 久久天 | 国产一级免费观看 | 一级影片在线观看 | 高潮毛片无遮挡 | 成人av免费在线看 | 蜜桃视频导航 | 亚洲精选一区二区 | 十八禁一区二区三区 | 亚洲熟妇无码另类久久久 | 夜色在线影院 | 日韩精品电影一区二区三区 | 欧美激情91 | 公侵犯一区二区三区 | 欧美福利一区二区三区 | 中文无码熟妇人妻av在线 | 桃色成人 | 奇米狠狠去啦 | 日韩精品一区二区三区不卡在线 | 欧美在线一区二区三区四区 | 福利在线视频观看 | 天天干天天爽天天射 |