Short version -
I need: byte a[] = {0x43, 0xE8, 0xD4, 0x7A};
to be in format: char b[] = "43E8D47A";
Longer version -
I have incoming serial data via a modbus sensor. a[] above is the meat of the payload as read in bytes. This converts it to float32 per the datasheet, which I have not been able to accomplish directly from a[]. I can accomplish the conversion using b[] and a union, which equals 465.65997. I cannot figure out how convert from a[] to b[].
edit: if there is a more elegant way to convert directly from a[] to the float value, I have looked with no luck and am all ears.
( 0x43 & 0xF ) + '0' = '3' // 2nd char --- need more is easy?
char b[ 3 ] = ( '4', '3', 0 };
The & is a bit-logical AND.
The >> is the right-shift operator that moves all the bits to the right.
The + '0' lines up numbers starting at zero to start at ASCII '0'.
You don't really need to convert from byte to char, what you need is to reverse the order of the bytes. If you are not concerned with keeping the original data in a, the reordering can be done in-place, or you can create a temporary byte array, then do a memcpy into the float (I'm not sure individually copying the bytes from a into the float would be a legal method of type punning).
0x43 << 24 + 0xE8 << 16 + 0xD4 << 8 + 0x7A = 0x43E8D47A = 1139332218 (my Linux calculator does HEX and DEC).
Note that once the variable is set to the HEX, it prints DEC by default.
What a 32 bit float will do with that is get the 1st 7 digits right and kludge the other 3 but that's what floats do.
A loop to do that will be cleaner. I leave that up to you.