我的程序从文件中读入 4 个字节的 IEEE 754 float 。我需要将这些字节可移植地转换为我的 C 编译器浮点类型。换句话说,我的 C 程序需要一个原型(prototype)为 float IEEE_754_to_float(uint8_t raw_value[4])
的函数。
最佳答案
如果您的实现可以保证正确的字节顺序:
float raw2ieee(uint8_t *raw)
{
// either
union {
uint8_t bytes[4];
float fp;
} un;
memcpy(un.bytes, raw, 4);
return un.fp;
// or, as seen in the fast inverse square root:
return *(float *)raw;
}
关于c - 如何将存储 IEEE 754 float 的 4 个字节转换为 C 中的浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242187/