[英]Converting 2 uint16_t to 32-float IEEE-754 fomat
I have a modbus devices which saves 32-bit float values in IEEE754 format.我有一个 modbus 设备,它以 IEEE754 格式保存 32 位浮点值。 In arduino, which uses C ofcourse, I can retrieve those values seperated into two 16-bit values, which are returned in uint16_t values.
当然,在使用 C 的 arduino 中,我可以检索分隔成两个 16 位值的那些值,这些值在 uint16_t 值中返回。 (each register from the modbus device has 16-bits, so the value is split over two registers, but the library I use returns uint16_t).
(来自 modbus 设备的每个寄存器都有 16 位,因此该值分为两个寄存器,但我使用的库返回 uint16_t)。
I am now trying to figure out to get these two uint16_t's into 1 32-bit float, and I would like to get some help, because I am stuck at how to convert this into this type of value.我现在正试图弄清楚如何将这两个 uint16_t 转换为 1 个 32 位浮点数,我想得到一些帮助,因为我一直不知道如何将其转换为这种类型的值。
Thanks in advance for your help.在此先感谢您的帮助。
EDIT:编辑:
To clarify, here's a format image为了澄清,这是一个格式图像
The only way around strict aliasing or invalid type-punning is really through the use of std::memcpy
:解决严格别名或无效类型双关的唯一方法实际上是通过使用
std::memcpy
:
uint16_t const value[2] = { low_word, high_word }; // Assuming little-endianness
float f;
std::memcpy(reinterpret_cast<void*>(&f), reinterpret_cast<void const*>(value), sizeof f);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.