小编Sve*_*-de的帖子

将unsigned解释为已签名

我正在研究嵌入式平台(ARM),在处理位模式时必须小心.让我们假装这条线超出了我的影响力:

uint8_t foo = 0xCE;          // 0b11001110
Run Code Online (Sandbox Code Playgroud)

解释为无符号,这将是206.但实际上它是签名的,因此类似于-50.如何继续使用此值作为签名?

int8_t bar = foo;            // doesn't work
Run Code Online (Sandbox Code Playgroud)

两者都没有(导致所有输入值的0x10或0x00)

int8_t bar = static_cast<int8_t>(foo);
int8_t bar = reinterpret_cast<int8_t&>(foo);
Run Code Online (Sandbox Code Playgroud)

我只是希望这些位保持不变,即. (bar == 0xCE)

反之亦然我感兴趣的是如何将代表负数的位模式转换为无符号变量而不会弄乱位模式.我正在使用GCC.

c++ embedded static-cast reinterpret-cast

7
推荐指数
2
解决办法
2376
查看次数

标签 统计

c++ ×1

embedded ×1

reinterpret-cast ×1

static-cast ×1