Getting the IEEE Single-precision bits for a float

后端 未结 4 1757
长情又很酷
长情又很酷 2021-01-07 03:54

I need to write an IEEE single-precision floating point number to a 32-bit hardware register at a particular address. To do that, I need to convert a variable of type float

4条回答
  •  不要未来只要你来
    2021-01-07 04:09

    If you're trying to simply display the integral value of the float as it's stored in memory, then try using a union:

    union {
        float a;
        unsigned int target;
    } u;
    

    Store the float value:

    u.a = 2.39;
    

    Print both float and integer values:

    printf ("a = %f\n", u.a);
    printf ("target = %08X\n", u.target); /* you could use %u to show decimal */
    

    No compiler warnings. I use GNU compiler (gcc) on Linux. Notice that target is not a pointer; this is the beauty (and hideousness) of unions. ;-)

提交回复
热议问题