I was looking for solution how convert float value to binary in STM32 and I found this:
float foo = 1.23;
uint32_t bar;
bar = *((uint32_t *)&foo);
My question is how does it works? I know it's some kind of pointer conversion, but I don't understand how precisely does it works.
Best Answer
Literally nothing happens. The
float
is already in a binary representation; the cast merely tells the compiler that it should treat the 32 bits in the variable as a 32 bit unsigned integer instead of a single precision floating point value.