C++ MSB到LSB交换后保留符号和小数

C++ MSB到LSB交换后保留符号和小数,c++,c,c++11,C++,C,C++11,我得到的数据是2字节短值中的十六进制值,但交换后的值丢失了 signed short value = 0x0040; value = (value*0.5) - 40; convertMSBTOLSB(value); //Conversion used bcz my device reading as LSB first //Implementation of convertMSBTOLSB(value) unsigned short temp = ((char*) &value

我得到的数据是2字节短值中的十六进制值,但交换后的值丢失了

 signed short value = 0x0040;
 value = (value*0.5) - 40;
 convertMSBTOLSB(value); //Conversion used bcz my device reading as LSB first

//Implementation of convertMSBTOLSB(value)
unsigned short temp = ((char*) &value)[0];  // assign value's LSB
temp = (temp << 8) | ((char*) &value)[1];   // shift LSB to MSB and add value's MSB
value = temp;
转换后,我得到的值为-8

发送0x51时出现问题,最终值应为0.5,但由于该值符号较短,因此为零

convertMSBTOLSB只是字节交换,如何处理代码以便它能够解析-ve和十进制值


如果需要一些输入来更改代码,使其能够同时解析-ve和十进制值,则不会得到0.5,因为您的值变量声明为短,因此只能保存整数。

您的问题不清楚。你写了MSBTOLSB在MSB和LSB之间交换,你也写了

convertmbstolsb只是字节交换

由于MSB和LSB引用位而不是字节,我真的不明白您在这里试图交换什么。

您的值应该更改其类型。0.5和值是不兼容的整型和浮点型。因此,操作值*0.5将导致零


除此之外,40将被提升为双精度,因此在将其分配回值后,该值将被截断。

如果您无法更改值的类型,那么您希望如何存储小数?本文的目标确实需要更多解释详细信息:40将被提升为双精度。0.5是双精度的,不是浮动的。正确的chux。谢谢你澄清这一点。