Interpretation of uint8_t/uint16_t/uint32_t/uint64_t data type centralized network

Keywords: Attribute xml ascii C

What data type is uint8_t/uint16_t/uint32_t/uint64_t? It is often encountered in embedded programming.

First

#define uint unsigned int;
The difference between int and uint is that one is signed and the other is unsigned.
uint is often used in microcontroller to define an unsigned integer variable.


Types ending with *_t

It is a structure annotation, which can be understood as the abbreviation of type/typedef, indicating that it is defined by typedef, not other data types.

Uint 8_t, uint 16_t, uint 32_t and so on are not new data types, they just use typedef to give the type alias, the trick of new bottled old wine. Nevertheless, don't underestimate typedef, it will be very useful for the maintenance of your code. For example, there is no bool in C, so in a software, some programmers use int, some programmers use short, it will be confusing, the best is to use a typedef to define, such as:
typedef char bool;

Generally speaking, a C project must do some work in this area, because you will involve cross-platform, different platforms will have different word lengths, so using precompiled and typedef can let you most effectively maintain your code. For the convenience of users, the C language hardware of C99 standard defines these types for us. We can safely use them.

According to posix standard, the corresponding *_t type of general shaping is:
1 byte uint8_t
2 bytes uint16_t
4 bytes uint32_t
8 bytes uint64_t

It seems that there is no such data type in C language, but in the process of practical application, it is found that many people have this representation in their code. In fact, uintX-t is defined by typedef. Pre-compiler and typedef can improve efficiency and facilitate code migration. The summary is as follows:

typedef unsigned char uint8_t; / / unsigned 8 digits

typedef signed char int8_t; / / signed 8 digits

typedef unsigned int uint 16_t; / unsigned 16 digits

typedef signed int int 16_t; / / signed 16 digits

typedef unsigned long uint32_t; / / unsigned 32 digits

typedef signed long int 32_t; / / signed 32 digits

Typeedef float float32; / single-precision floating-point number

Typeedef double float64; / double precision floating point number

Generally speaking, the corresponding *_t type of shaping is:
uint8_t is 1 byte.

uint16_t is 2 bytes

uint32_t is 4 bytes.

uint64_t is 8 bytes.

It is not difficult to see that uint8_t is defined by header file X.h. In fact, the compiler actually treats it as "char" and operates on character variables. For reference only, please point out any mistakes.



What data type is uint8_t/uint16_t/uint32_t/uint64_t?

These data types are defined in C99 and are specifically defined in: / usr/include/stdint.h ISO C99: 7.18 Integer types < stdint. H >

  1. /* There is some amount of overlap with <sys/types.h> as known by inet code */  
  2. #ifndef __int8_t_defined  
  3. # define __int8_t_defined  
  4. typedef signed char             int8_t;   
  5. typedef short int               int16_t;  
  6. typedef int                     int32_t;  
  7. # if __WORDSIZE == 64  
  8. typedef long int                int64_t;  
  9. # else  
  10. __extension__  
  11. typedef long long int           int64_t;  
  12. # endif  
  13. #endif  
  14.   
  15. /* Unsigned.  */  
  16. typedef unsigned char           uint8_t;  
  17. typedef unsigned short int      uint16_t;  
  18. #ifndef __uint32_t_defined  
  19. typedef unsigned int            uint32_t;  
  20. # define __uint32_t_defined  
  21. #endif  
  22. #if __WORDSIZE == 64  
  23. typedef unsigned long int       uint64_t;  
  24. #else  
  25. __extension__  
  26. typedef unsigned long long int  uint64_t;  
  27. #endif  

Format output:

unit64_t     %llu   

unit32_t     %u

unit16_t    %hu

Be careful:

You have to be careful about the output of uint8_t type variables, such as the following code, what will be output?

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

The result is: field=C, not field=67 as we think.

This is due to typedef unsigned char uint8_t;

uint8_t is actually a char, cerr will output the ASCII code as a character of 67, not a number of 67.

Therefore, a variable of type uint8_t actually outputs its corresponding character, not its real number.

To output 67, you can do this:

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

The result is: field=67

Similarly: when uint8_t type variables are converted into strings and strings into uint8_t type variables, it should be noted that when uint8_t type variables are converted into strings, the corresponding characters of ASCII codes will be obtained. When strings are converted into uint8_t variables, the first character of the string will be assigned to variables.

For example, the following code:

  1. #include <iostream>  
  2. #include <stdint.h>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6.   
  7. int main()  
  8. {  
  9.     uint8_t fieldID = 67;  
  10.   
  11.     // uint8_t --> string  
  12.     string s;  
  13.     ostringstream strOStream;  
  14.     strOStream << fieldID;  
  15.     s = strOStream.str();  
  16.     cerr << s << endl;  
  17.       
  18.     // string --> uint8_t  
  19.     s = "65";   
  20.     stringstream strStream;  
  21.     strStream << s;  
  22.     strStream >> fieldID;  
  23.     strStream.clear();  
  24.     cerr << fieldID << endl;  
  25. }  

The output of the above code is:

C

6

Self-understanding

The following are some definitions of typedef naming in the stdint.h header file of CodeBlock compiler environment for uint8_t and so on.

  1. /* 7.18.1.1  Exact-width integer types */  
  2. typedef signed char int8_t;  
  3. typedef unsigned char   uint8_t;  
  4. typedef short  int16_t;  
  5. typedef unsigned short  uint16_t;  
  6. typedef int  int32_t;  
  7. typedef unsigned   uint32_t;  
  8. __MINGW_EXTENSION typedef long long  int64_t;  
  9. __MINGW_EXTENSION typedef unsigned long long   uint64_t;  
  10.   
  11. /* 7.18.1.2  Minimum-width integer types */  
  12. typedef signed char int_least8_t;  
  13. typedef unsigned char   uint_least8_t;  
  14. typedef short  int_least16_t;  
  15. typedef unsigned short  uint_least16_t;  
  16. typedef int  int_least32_t;  
  17. typedef unsigned   uint_least32_t;  
  18. __MINGW_EXTENSION typedef long long  int_least64_t;  
  19. __MINGW_EXTENSION typedef unsigned long long   uint_least64_t;  
  20.   
  21. /*  7.18.1.3  Fastest minimum-width integer types  
  22.  *  Not actually guaranteed to be fastest for all purposes  
  23.  *  Here we use the exact-width types for 8 and 16-bit ints.  
  24.  */  
  25. typedef signed char int_fast8_t;  
  26. typedef unsigned char uint_fast8_t;  
  27. typedef short  int_fast16_t;  
  28. typedef unsigned short  uint_fast16_t;  
  29. typedef int  int_fast32_t;  
  30. typedef unsigned  int  uint_fast32_t;  
  31. __MINGW_EXTENSION typedef long long  int_fast64_t;  
  32. __MINGW_EXTENSION typedef unsigned long long   uint_fast64_t;  
  33.   
  34. /* 7.18.1.5  Greatest-width integer types */  
  35. __MINGW_EXTENSION typedef long long  intmax_t;  
  36. __MINGW_EXTENSION typedef unsigned long long   uintmax_t;  

Posted by ctcp on Tue, 01 Jan 2019 02:06:08 -0800