Hex to float

Started by
10 comments, last by spider111 22 years, 7 months ago
There is one more thing I should say about my code snipped for the sake of compleateness.

The code above assumes that the endien-ness of you byte stream and memory are the same. This is normally the case but not always true. For example, the stream could be comming from a file saved in little endian format (like and intel PC) and being processed on big endian machine (like a Mac)... in that case, I think you invert the indices in the stream like so
foo.bytes[0] = stream[3];foo.bytes[1] = stream[2];... 

but I''m not 100% sure on that.

---
PAGE FAULT: Please insert "Swap File, Disk 2"
and press any key to continue.
---
PAGE FAULT: Please insert "Swap File, Disk 2"
and press any key to continue.
Advertisement
Just for kicks...

If, for sake of clarity, we considered an 8-bit figure read in two 4-bit halves
// stream is ''abcdefgh''v[0] = stream[0]; // v[0] is efgh on little-endian machinesv[1] = stream[1]; // v[1] is abcd 

Reading in reverse order yields:
v[0] = stream[0]; // v[0] is abcd on little-endian machinesv[1] = stream[1]; // v[1] is efgh 

The proper solution lies in creating some form of mask and swapping bits that are non-symmetric about the midpoint. Or something like that.

This topic is closed to new replies.

Advertisement