Subj : Re: reading Java floats from C To : comp.lang.c,comp.programming From : rem642b Date : Tue Jun 28 2005 06:48 pm (I've cross-posted this to comp.programming where it's more relevant. Also I've blacked out the specific names of programming languages because that's irrelevant to my general answer.) > From: sbalko@gmail.com > I am trying to read ###-floats (IEEE ??? encoding) stored in a binary > file from %%% (??? on ???, more specifically). Unfortunately, %%% seems > to expect floats to be stored somewhat differently than ### does. I > suspected an endianess problem and tried out ntohl/htonl but it > doesn't help. Any clues? If you can't find such an answer from online documents, why didn't you just do some experiments? For example, try this to see how ### writes floats in binary mode: Write a test program that writes out exactly five values of exactly 0.0, then write out these values in sequence: 9.0 0.0 10.0 0.0 11.0 0.0 12.0 0.0 13.0 0.0 14.0 0.0 15.0 0.0, and then examine the resultant file to see if you can find: - The same exact pattern repeating exactly five times before it's broken by other patterns not the same, to show you what the 0.0 looks like in binary file format. - Alternating original pattern and other patterns the same length, to make sure you haven't accidently used different precision for the non-zero values generated from the index variable in your loop and the zero values generated by literals. - Among those non-zero groups of bytes, see if you can find a bit pattern that goes somewhat like this: 1001 1010 1011 1100 1101 1110 1111 The '1' might be missing if it's in a notation where the 1 is assumed rather than explicit, but the other bits should follow that pattern. At that point you have a good idea where the mantissa is located. Now to find where the exponent is located, generate this sequence: 0.0 1.0 0.0 2.0 0.0 4.0 0.0 8.0 0.0 16.0 0.0 32.0 0.0 64.0 0.0 You should see a similar pattern in the bits. Finally you need to know how negative numbers are expressed. I leave that as an exercise for the reader. Once you know all that for ###, do the same for %%%. Write that test data from the program that will be doing reading. (Unless it's totally broken, it should write data in the same layout that it expects to read it in.) Now compare what you learned about ### and %%%, whether sequence of bytes is the only difference, or there's a more complicated difference in representation. .