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

Retain sign and decimal after MSB to LSB swaping

本文关键字:保留 符号 小数 交换 MSB LSB      更新时间:2023-10-16

我在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,因为您的value变量声明为短,因此只能保存整数。

你的问题不清楚。你写了convertMSBTOLSB在MSB和LSB之间转换,你还写了

convertMSBTOLSB只是字节交换

由于MSB和LSB指的是Bits而不是Bytes,我真的不明白你在这里想交换什么

您的value应该更改其类型。0.5value是不兼容的类型(整数和浮点)。因此,操作(value*0.5)的结果将是零。

除此之外,40将被提升为double,因此在将其赋值给value后,该值将被截断。