How can I tell the compiler that it is an IEEE 754?

1

I need help.

I have, for example, a hexadecimal number 0x41140000 that is 9.25 by the IEEE 754 standard, but how to read this value (contained in an integer variable) giving cast to a float and pick up the correct value, ie 9.25. / p>

What is happening is that after doing the assignment float = 0x41140000 I get 1091829760.00 instead of the desired value, which even makes sense, but it's not what I want.

Does anyone know a method to do this? Thanks in advance for your cooperation!

    
asked by anonymous 14.03.2017 / 03:51

1 answer

1

As you've noticed, if you attempt to do a typecast , C will try to perform the conversion as if the number were originally integer. What you need is the C equivalent of reinterpret_cast<> of C ++.

In pure C, this is obtained with a union ; having an integer and a floating point, just write to the integer and read the floating point:

float
reinterpret(uint32_t integer) {
    union { float f; uint32_t i; } u;
    u.i = integer;
    return u.f;
}

The inverse process uses the same structure, but reads the integer and writes the floating point.

    
14.03.2017 / 18:10