Hi,
A plea to the super smart (which I'm not)...
I have to read a file written by a Java program using a C program, but
I'm all confused as to how to get the data (numbers) out. Has anyone an
idea how to translate what the Java method writeDouble() has written
back into a C double variable?
For example, the Java prog has written hexadecimal 0x401F972E5CB972E6,
and I think this particular value represents decimal 200.6, but how?
Argh! So confusing!
Thanks in anticipation,
Pete.
Oliver Wong - 27 Jul 2006 15:19 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> For example, the Java prog has written hexadecimal 0x401F972E5CB972E6,
> and I think this particular value represents decimal 200.6, but how?
Are you sure it's not 7.897637795275591 ?
The method you're using encodes to IEEE-754 floating point. See
http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex64.html
- Oliver
Pete - 28 Jul 2006 20:49 GMT
> Are you sure it's not 7.897637795275591 ?
Yes you are right! It was a value in inches, it needed converting to
millimetres (x 25.4)...
Sorry, and thnks for the hint towards IEEE 754.
Cheers,
Pete
Patricia Shanahan - 27 Jul 2006 19:41 GMT
> I have to read a file written by a Java program using a C program, but
> I'm all confused as to how to get the data (numbers) out. Has anyone an
[quoted text clipped - 3 lines]
> For example, the Java prog has written hexadecimal 0x401F972E5CB972E6,
> and I think this particular value represents decimal 200.6, but how?
Any double whose hex representation starts "401" must be in the range 4
<= x < 8.
If you are using any modern machine, the C double representation is
likely to be 64-bit IEEE 754, like Java.
However, you may need to do some shuffling to get into little-endian
format before you can actually convert to double. If you have any
difficulty, split the problem into two parts:
1. Transfer a 64 bit integer, preserving its value.
2. Construct a 64 bit integer representing a floating point number, and
convert it to double in C. 0x3ff0000000000000 is 1.0. I used to do this
using a union or by casting a pointer, but there may be better methods now.
Patricia
Pete - 28 Jul 2006 20:51 GMT
> using a union or by casting a pointer, but there may be better methods now.
Yes, that's how I've solved it now. The key was to find out the IEEE
754 information. Then using a union of a double and the 8 bytes of
data from the file.
Thanks,
Pete.