Conversion from int16_t to uint32_t

本文详细解释了将int16_t类型值转换为uint32_t类型值的过程,说明了结果的确定性和转换规则,强调了标准中对整数转换的定义,并讨论了使用int_least16_t和uint_least32_t的必要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int16_t s;
uint32_t ui = s;

Is the result of converting a int16_t value to a uint32_t value compiler dependent? If not, what's the rule?

=================================================================================================================

The results are well-defined; non-negative values stay the same, and negative values are reduced modulo 2^32. But the situations where exact sized types like int16_t and uint32_t are quite rare. There's really no need for anything other than int and unsigned long here: those types have at least as many bits as int16_tand uint32_t and, unlike int16_t and uint32_t, they're required to exist on any conforming implementation. If you really really want the sexy new sized types, at least go for portability withint_least16_t and uint_least32_t.

=================================================================================================================

 look for "integer conversions" in the standard. The difference between uint32_t anduint_least32_t here is precisely that the value of a converted int16_t is fully determined by the standard with uint32_t, whereas with uint_least32_t it depends on the width of the type (and only on that, since once the implementation decides that the rest of the behavior is determined). In the unlikely event that you want to convert negative values to an unsigned type, chances are you do want to know the width of the type. – Steve Jessop

=================================================================================================================




Arduino:1.8.19 (Windows 10), 开发板:"ESP32 Dev Module, Disabled, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 115200, Core 1, Core 1, None, Disabled, Disabled" D:\TFT\Demo_Arduino\Demo_UNO_Hardware_SPI\Example_10_touch_pen\touch_pen\touch_pen.ino: In function 'void show_color_select_menu()': touch_pen:94:16: error: invalid conversion from 'const char*' to 'uint8_t*' {aka 'unsigned char*'} [-fpermissive] 94 | show_string("OK",CENTER,COLORBOXSIZE/2+4,2,RED, BLACK,1); | ^~~~ | | | const char* D:\TFT\Demo_Arduino\Demo_UNO_Hardware_SPI\Example_10_touch_pen\touch_pen\touch_pen.ino:74:27: note: initializing argument 1 of 'void show_string(uint8_t*, int16_t, int16_t, uint8_t, uint16_t, uint16_t, boolean)' 74 | void show_string(uint8_t *str,int16_t x,int16_t y,uint8_t csize,uint16_t fc, uint16_t bc,boolean mode) | ~~~~~~~~~^~~ D:\TFT\Demo_Arduino\Demo_UNO_Hardware_SPI\Example_10_touch_pen\touch_pen\touch_pen.ino: In function 'void show_pen_size_select_menu()': touch_pen:113:16: error: invalid conversion from 'const char*' to 'uint8_t*' {aka 'unsigned char*'} [-fpermissive] 113 | show_string("OK",CENTER,COLORBOXSIZE/2+4,2,RED, BLACK,1); | ^~~~ | | | const char* D:\TFT\Demo_Arduino\Demo_UNO_Hardware_SPI\Example_10_touch_pen\touch_pen\touch_pen.ino:74:27: note: initializing argument 1 of 'void show_string(uint8_t*, int16_t, int16_t, uint8_t, uint16_t, uint16_t, boolean)' 74 | void show_string(uint8_t *str,int16_t x,int16_t y,uint8_t csize,uint16_t fc, uint16_t bc,boolean mode) | ~~~~~~~~~^~~ D:\TFT\Demo_Arduino\Demo_UNO_Hardware_SPI\Example_10_touch_pen\touch_pen\touch_pen.ino: In function 'void show_main_menu()': touch_pen:124:16: error: invalid conversion from 'const char*' to 'uint8_t*' {aka '
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值