简体   繁体   English

将 2 uint16_t 转换为 32-float IEEE-754 格式

[英]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为了澄清,这是一个格式图像IEEE754

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.

相关问题 C ++最佳实践:接受uint8_t,uint16_t,uint32_t,float的函数 - C++ Best Practice: function to accept uint8_t, uint16_t, uint32_t, float SIMD-&gt; uint16_t数组使float数组在float上工作,然后返回uint16_t - SIMD -> uint16_t array to float array work on float then back to uint16_t 32位,64位和80位浮点IEEE-754的可表示值范围? - Range of representable values of 32-bit, 64-bit and 80-bit float IEEE-754? 防止将uint64_t转换为uint16_t - Prevent converting uint64_t to uint16_t 如何检测非 IEEE-754 浮点数,以及如何使用它们? - How to detect non IEEE-754 float, and how to use them? 从unsigned int转换为uint16_t的转换错误 - Casting error converting from unsigned int to uint16_t IEEE-754的浮点数,双精度数和四进制数是否保证精确表示-2,-1,-0、0、1、2? - Does IEEE-754 float, double and quad guarantee exact representation of -2, -1, -0, 0, 1, 2? 如何输出IEEE-754格式的整数作为浮点数 - How to output IEEE-754 format integer as a float 防止不同数据类型上的重复代码(uint16_t / uint32_t) - Prevent duplicated code on different data types (uint16_t/ uint32_t) 为STM32编译时“从&#39;uint8_t *&#39;广播到&#39;uint16_t&#39;失去精度” - “cast from 'uint8_t*' to 'uint16_t' loses precision” when compiling for STM32
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM