我怎么能转换4个字节存储一个IEEE 754浮点数在C浮点值? [英] How can I convert 4 bytes storing an IEEE 754 floating point number to a float value in C?

查看:789
本文介绍了我怎么能转换4个字节存储一个IEEE 754浮点数在C浮点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序读入4个字节从文件中读取IEEE 754浮点数。我需要携带转换到我的C语言编译器这些字节float型。换句话说,我需要一个函数原型浮动IEEE_754_to_float(uint8_t有RAW_VALUE [4])我的C程序。

My program reads into 4 bytes an IEEE 754 floating point number from a file. I need to portable convert those bytes to my C compilers float type. In other words I need a function with the prototype float IEEE_754_to_float(uint8_t raw_value[4]) for my C program.

推荐答案

如果您的实现可以保证正确的顺序:

If your implementation can guarantee correct endianness:

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;
}

这篇关于我怎么能转换4个字节存储一个IEEE 754浮点数在C浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆