Unexpected hex value for float

2 weeks ago 18
ARTICLE AD BOX

I have discovered I'm getting unexpected hex values representing floats on a ARM STM32H753 processor. I cannot determine why these hex values are being produced. It is a single precision float.

Here is simplified code:

#include <stdio.h> #include <cstdint> int main() { float setVal = 50000.0f; uint32_t * val1 = (uint32_t *)((void *) (& setVal)); printf("Float into vSV: %x\r\n", * val1); }

In the real code it is being run on a RTOS, so the code is a bit more complicated and it uses another function instead of printf called chprintf.

The code basically is designed to set a variable to a value (50000.0f), to get the address, eventually cast to uint32_t, and then print out the 4 bytes of the uint32_t.

The result printed is 40E86A00. However what I would expect to be printed is 47435000. I expect that value based on using https://gregstoll.com/~gregstoll/floattohex/ to convert 50000.0 to a hex value.

I also have some library code which sends the float across the UART. The data transmitted on the UART is also the 40E86A00 (but with the byte order swapped due to endianness).

If I run this code on a Intel based Linux PC, then the result is 47435000 which matches to my expectation.

What am I missing?

Read Entire Article