您的位置:首页 > 产品设计 > UI/UE

uint8_t / uint16_t / uint32_t /uint64_t 数据类型集中网上的解释

2018-02-22 21:09 791 查看
uint8_t / uint16_t / uint32_t /uint64_t  是什么数据类型,在嵌入式编程中经常会遇见。
首先

#define uint unsigned int;int和uint的区别:一个有符号,一个无符号。uint在单片机中经常用到,定义一个无符号整型变量。

以*_t结尾的类型
它就是一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是其它数据类型。uint8_t,uint16_t,uint32_t等都不是什么新的数据类型,它们只是使用typedef给类型起的别名,新瓶装老酒的把戏。不过,不要小看了typedef,它对于你代码的维护会有很好的作用。比如C中没有bool,于是在一个软件中,一些程序员使用int,一些程序员使用short,会比较混乱,最好就是用一个typedef来定义,如:
typedef char bool;一般来说,一个C的工程中一定要做一些这方面的工作,因为你会涉及到跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以让你最有效的维护你的代码。为了用户的方便,C99标准的C语言硬件为我们定义了这些类型,我们放心使用就可以了。 按照posix标准,一般整形对应的*_t类型为:
1字节     uint8_t
2字节     uint16_t
4字节     uint32_t
8字节     uint64_tC语言中好像没有这种数据类型,但是在实际应用的过程中,发现许多人的代码中都存在这种表示方式。其实uintX-t就是通过typedef定义的,利用预编译和typedef可提高效率也方便代码移植。总结如下:typedef unsigned char   uint8_t;     //无符号8位数    typedef signed   char   int8_t;      //有符号8位数    typedef unsigned int    uint16_t;    //无符号16位数    typedef signed   int    int16_t;     //有符号16位数    typedef unsigned long   uint32_t;    //无符号32位数    typedef signed   long   int32_t;     //有符号32位数    typedef float           float32;     //单精度浮点数typedef double          float64;     //双精度浮点数一般来说整形对应的*_t类型为:
    uint8_t为1字节        uint16_t为2字节      uint32_t为4字节        uint64_t为8字节    不难看出,通过头文件X.h定义了uint8_t,其实编译器实际上是把它作为"char"来处理的,在对字符型的变量进行操作。以上仅做参考,有错误请指出。

uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
这些数据类型是 C99 中定义的,具体定义在:/usr/include/stdint.h    ISO C99: 7.18 Integer types <stdint.h>[objc] view plain copy/* 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     %uunit16_t    %hu注意:必须小心 uint8_t 类型变量的输出,例如如下代码,会输出什么呢?uint8_t fieldID = 67;
cerr<< "field=" << fieldID <<endl;
结果发现是:field=C 而 不是我们所想的 field=67这是由于 typedef unsigned char uint8_t; 
uint8_t 实际是一个 char, cerr << 会输出 ASCII 码是 67 的字符,而不是 67 这个数字.因此,输出 uint8_t 类型的变量实际输出的是其对应的字符, 而不是真实数字.若要输出 67,则可以这样:cerr<< "field=" << (uint16_t) fieldID <<endl;
结果是:field=67同样: uint8_t 类型变量转化为字符串以及字符串转化为 uint8_t 类型变量都要注意, uint8_t类型变量转化为字符串时得到的会是ASCII码对应的字符, 字符串转化为 uint8_t 变量时, 会将字符串的第一个字符赋值给变量.例如如下代码:[html] view plain copy#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;  
}  

上述代码输出的是:C6

自己理解

一下是CodeBlock编译环境下stdint.h头文件中关于uint8_t等的一些定义typedef命名。
[html] view plain copy/* 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;  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐