Hi,
I have a binary file generated by a Delphi program. I can read all
strings (prefix length strings) but I can't get the float right.
Example: Somewhere in the Hex below store: 40.0 Single (Delphi = 4
bytes)
Hex From file: 00 00 00 00 20 42 00 00 00
- I assume to read 00 00 20 42 but can read from any pos ...
I have tried any type of read and conversion but can't get this one
right (or any floats) from the file.
Any an all help is much appreciated
EJP - 18 Oct 2006 01:46 GMT
> Hex From file: 00 00 00 00 20 42 00 00 00
>
> - I assume to read 00 00 20 42 but can read from any pos ...
I don't know what that last sentence means, but 00 00 20 42 looks like a
word-swap. Try swapping it around to 20 42 00 00, then if that doesn't
work try swapping the bytes too. There are a lot of permutations. If you
really want to get this right, find the specification of the Delphi
single-precision FP and work out how to map it to IEEE 754 which is what
Java uses.
Arne Vajhøj - 18 Oct 2006 02:06 GMT
> I have a binary file generated by a Delphi program. I can read all
> strings (prefix length strings) but I can't get the float right.
[quoted text clipped - 8 lines]
> I have tried any type of read and conversion but can't get this one
> right (or any floats) from the file.
import java.io.*;
public class FP {
public static void main(String[] args) throws IOException {
byte[] b = { 0x42, 0x20, 0x00, 0x00 };
DataInputStream dis = new DataInputStream(new
ByteArrayInputStream(b));
float x = dis.readFloat();
System.out.println(x);
}
}
outputs 40.0 !
Arne