关于嵌入式编程中的uint8

编程入门 行业动态 更新时间:2024-10-28 02:36:20

关于<a href=https://www.elefans.com/category/jswz/34/1770281.html style=嵌入式编程中的uint8"/>

关于嵌入式编程中的uint8

在嵌入式编程中经常遇到用uint8_t、uint16_t、uint32_t、uint_fast16_t之类的关键字定义一些整型变量,但是具体表示什么意思,并不是太清楚,只是把它当成int之类的整型变量定义关键字。在自己理解他们之前,先写一下在网上搜到的常见的对他们的解释。

常见解释(都是个人见解,不一定准确全面)

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

论坛上就有人问:以*_t结尾的类型是不是都是long型的?在baidu上查一下,才找到答案,这时才发觉原来自己对C掌握的太少。

那么_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_t

C语言中好像没有这种数据类型,但是在实际应用的过程中,发现许多人的代码中都存在这种表示方式。其实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>

/* 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;

结果发现是: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 变量时, 会将字符串的第一个字符赋值给变量.

例如如下代码:

#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编译环境下stdint.h头文件中关于uint8_t等的一些定义typedef命名。
/* 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

本文发布于:2023-06-15 01:26:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/717648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌入式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!