How do I warn the compiler that this is an IEEE 754?

Asked

Viewed 80 times

2

i need help.

I have, for example, a hexadecimal number 0x41140000 which is 9.25 by the IEEE 754 standard, but how to read this value (contained in an integer variable) by casting a float and picking the correct value, that is, 9.25.

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

Does anyone know a way to do that? I thank you for your cooperation!

1 answer

2


As you noticed, if you try to make one Typecast, the Runtime do C will try to perform the conversion as if the number was originally integer. What you need is the C equivalent of the reinterpret_cast<> of the C++.

In pure C, this is achieved with a union; having an entire member and a floating point, just write to the whole and read the floating point:

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

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

  • It was just what I was looking for, Wtrmude. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.