在MSB到LSB交换后保留符号和小数
Retain sign and decimal after MSB to LSB swaping
我在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.5
和value
是不兼容的类型(整数和浮点)。因此,操作(value*0.5)
的结果将是零。
除此之外,40将被提升为double
,因此在将其赋值给value
后,该值将被截断。
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 为多个会话保留XPtr
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- C++保留符号作为C变量名
- 尝试创建一个 3D 矢量,该矢量将保留指向指向无符号字符的指针的指针
- 如何在 c++ 中打印字母和符号后保留在一个位置
- 避免重复符号并在头文件中保留代码
- 在MSB到LSB交换后保留符号和小数
- 在 C++ 中,从无符号 int 到 int 的转换始终保留位模式