raviteja
raviteja

Reputation: 5

getting wrong values while reading Double from binary file in java

RandomAccessFile in = new RandomAccessFile("BOT.grd", "r");
in.seek(28);
double xll=in.readDouble();

The above is the code i am using to read double data which is present from 29 to 36 byte location.The value present is 500 but i am getting 2.088E-317

Upvotes: 1

Views: 1847

Answers (1)

Jörn Horstmann
Jörn Horstmann

Reputation: 34024

It seems this file is stored in a different Endianness than the one java uses. The bytes probably need to be reversed before converting to double, you could try the following code to read the value:

long   l = in.readLong();
double d = Double.longBitsToDouble(Long.reverseBytes(l));

Here is an example that illustrates the problem:

double d = 500.0;

long l1 = Double.doubleToLongBits(d);
long l2 = Long.reverseBytes(l1);

System.out.println(Double.longBitsToDouble(l1));
System.out.println(Double.longBitsToDouble(l2));

Output:

500.0
2.088356E-317

Upvotes: 1

Related Questions