I’m wondering what number each byte is in IEEE754(Arduino float). Though I know that float consists of 4byte, I don’t know how they could be expressed in decimal or hex.
Like float pi = 3.141592, could you tell me each byte in decimal or hex.
Also, by containing those numbers in uint32_t data[4], I think I can reconstruct pi as follows. Right?
The value you're building is NOT directly converted to a float as if the 4 bytes representing the value you need. The "or"s you use just build a "long" integer represented internally as 0xD80F4940, and when you try casting it to float it just use that 4 bytes as a whole number, not a float IEEE representation. In fact, that hex value is equivalent to the decimal 3624880448, and the "float" value you get is exactly that one.
If you want to get the original float from 4 bytes, you can use the reverse method I have shown you to get the 4 bytes out of the float:
// Reconstructing PI from hex values
pi.b[0] = 0xD8;
pi.b[1] = 0x0F;
pi.b[2] = 0x49;
pi.b[3] = 0x40;
Serial.println(pi.value, 6);
This is not the only way to do it, but the others involve addresses and pointers, less intuitive and straightforward than this one.
thank you!!!,
Actually, my aim is to receive 4byte from another device(RPi), and reconstruct it to float value.
So I want the code to reconstruct pi from the 4bytes. Could you give me any tip to make it?
you are shifting 0xd8 by 24 bytes ... but 0xD8 is byte[0] not byte[3]
Furthermore your casts will not work or better - will not do what you need.
See again the wokwi example, there are both lines ... one from float to byte[4], and one from byte[4] to float.
two simple lines ... just with memcpy
Thank you!
I think I could not use the memcpy example cus there is difference between SEEING the content of creceived and RECEIVING values then including into the content of creceived.
//just SEEING
for (int i = 0; i < 4; i++)
{
Serial.print(byte(creceived[i]), HEX); // what have we got
Serial.print(' ');
}
Actually, Rpi just sends as follows so I think the transmitter has no problem.
'''
spi.write([0xD8])
spi.write([0x0F])
spi.write([0x49])
spi.write([0x40])
'''