uint16_t转换成char_16bit转8bit

uint16_t转换成char_16bit转8bit简单来说,uint8_t/uint16_t/uint32_t/uint64_t这些数据类型都只是别名而来,具体如下:一、C语言数据基本类型在C语言中有6种基本数据类型:short、int、long、float、double、char1)整型:shortint、int、longint2)浮点型:float、double3)字符类型:char二、分析uint8_…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

简单来说,uint8_t / uint16_t / uint32_t /uint64_t这些数据类型都只是别名而来,具体如下:

一、C语言数据基本类型

在C语言中有6种基本数据类型:short、int、long、float、double、char

1)整型:short  int、int、long int

2)浮点型:float、double

3)字符类型:char


二、分析uint8_t\uint_16_t\uint32_t\uint64_t

1、数据来源:这些数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。也就是说,它们其实是我们已知的类型的别名。

2、typedef:typedef用来定义关键字或标识符的别名

3、使用原因:方便代码的维护。比如,在C中没有bool型,于是在一个软件中,一个程序员使用int,一个程序员使用short,会比较混乱,最好用一个typedef来定义一个统一的bool,每个程序员都可以用这个别名的bool。

不同的平台会有不同的字长,所以利用预编译和typedef可以方便的维护代码。

typedef unsigned char uint8_t;//将uint8_t别名为无符号字符型

4、定义:在C99标准中定义了这些数据类型,具体定义在:stdint.h中

uint16_t转换成char_16bit转8bit

定义类型如下:

typedef   signed          char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;
typedef   signed       __INT64 int64_t;

/* exact-width unsigned integer types */
typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;
typedef unsigned       __INT64 uint64_t;

/* 7.18.1.2 */
/* smallest type of at least n bits */
/* minimum-width signed integer types */
typedef   signed          char int_least8_t;
typedef   signed short     int int_least16_t;
typedef   signed           int int_least32_t;
typedef   signed       __INT64 int_least64_t;

/* minimum-width unsigned integer types */
typedef unsigned          char uint_least8_t;
typedef unsigned short     int uint_least16_t;
typedef unsigned           int uint_least32_t;
typedef unsigned       __INT64 uint_least64_t;

/* 7.18.1.3 */
/* fastest minimum-width signed integer types */
typedef   signed           int int_fast8_t;
typedef   signed           int int_fast16_t;
typedef   signed           int int_fast32_t;
typedef   signed       __INT64 int_fast64_t;

/* fastest minimum-width unsigned integer types */
typedef unsigned           int uint_fast8_t;
typedef unsigned           int uint_fast16_t;
typedef unsigned           int uint_fast32_t;
typedef unsigned       __INT64 uint_fast64_t;

/* 7.18.1.4 integer types capable of holding object pointers */
#if __sizeof_ptr == 8
typedef   signed       __INT64 intptr_t;
typedef unsigned       __INT64 uintptr_t;
#else
typedef   signed           int intptr_t;
typedef unsigned           int uintptr_t;
#endif

/* 7.18.1.5 greatest-width integer types */
typedef   signed     __LONGLONG intmax_t;
typedef unsigned     __LONGLONG uintmax_t;

5、格式化输出:

1)uint16_t %hu

2)uint32_t %u

3)uint64_t %llu

6、uint8_t类型的输出:

typedef unsigned char uint8_t;//将uint8_t别名为无符号字符型

uint8_t buf = 65;

printf("buf = %d",buf);//错误
printf("buf = %c",buf);//正确,打印出字符的ASCII码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/196239.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号