gpt4 book ai didi

C++ 将无符号转换为有符号

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:24 27 4
gpt4 key购买 nike

我试图复制 this但它不会编译。

unsigned char x = 0;
reinterpret_cast<signed char>(x);

它说

C:\Users\SXG5558\Documents\Arduino\sketch_jun30a\sketch_jun30a.ino: In function 'void setup()':

sketch_jun30a:3: error: invalid cast from type 'unsigned char' to type 'signed char'

reinterpret_cast<signed char>(x);

^

exit status 1

编辑:

明确地说,我真的很想将内存中的位从有符号重新解释为无符号。我正在编写一个读取和写入未签名数据的 I2C 库,但我正在使用该库来控制已签名的传感器,因此我想重新解释转换实际数据。

最佳答案

相关部分来自cppreference.com

Type aliasing

When a pointer or reference to object whose dynamic type is DynamicType is reinterpret_cast (or C-style cast) to a pointer or reference to object of a different type AliasedType, the cast always succeeds, but the resulting pointer or reference may only be used to access the object if one of the following is true:

[...]

  • AliasedType is the (possibly cv-qualified) signed or unsigned variant of DynamicType

[...]

即。您忘记了链接示例中的 &

关于C++ 将无符号转换为有符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133462/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com