A piece of code I'm trying to debug generated a sequence of 4 bytes from a known float number. I'm trying to verify the float number given the four bytes.

The (known) float 8.03 generated the bytes 56,46,48,51 (here, it is listed according to how it was received in a buffer i.e., 56 first, then 46, then 48, then 51). But I would like to know if 56-46-48-51 (MSbyte to LSbyte) or 51-48-46-56 represents 8.03 (if it does, at all).

I'm not sure how to write code to check both cases. Can someone offer a hint?


1 Answer 1


Seems there is a principal misconception here, because, citing you,

The (known) float 8.03 generated the bytes 56,46,48,51

the sequence 56,46,48,51 is 8.03 in text (ASCII), but not the binary representation according to widely used standards as IEEE754. With IEEE754 4-byte binary, 8.03 is represented as 65,0,122,225 (in big-endian order) or 225,122,0,65 (in little-endian order, as on x86).

If it is the truly representation of this float, then

1) You can't be sure every float in text is represented in 4 bytes, unless this is stated by some spec; also please consider how the representation text is terminated;

2) The issues like "MSbyte to LSbyte" aren't usually applicable to ASCII text in RAM, because text is stored nearly always that offset 0 carries the leftmost character. Exceptions are too rare and shall be explicitly specified. But this text property doesn't apply to integer reading of the same RAM. If you read "8.03" as integer, on little-endian machines as x86 it results in 0x33302e38.

And, answering your header question "How to convert byte-array (4 bytes) back into float?", the answer, based on your example, is to call a standard library routine which parses a text form. If it's written in C, it's strtod or sscanf, and you shall provide NUL-termination of the input character array.

Not the answer you're looking for? Browse other questions tagged or ask your own question.