1
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

5
  • 6
    There's something wrong in either the code reading the file or the code writing the file. That's all we can say without more details/code/examples. Please see tinyurl.com/so-hints Commented Nov 7, 2011 at 14:30
  • 7
    Create an SSCCE Commented Nov 7, 2011 at 14:31
  • 2
    A shot in the dark: read floats instead of doubles? Commented Nov 7, 2011 at 14:31
  • 1
    Can you give more specifics about how you are reading and writing the file? Given that you are using RandomAccessFile, I guess you are not using serialization. So how are you writing and then reading the data? How about a code snippet for each? Commented Nov 7, 2011 at 15:06
  • I had give my code above. I was unable to read double values its working fine with integer values Commented Nov 7, 2011 at 16:12

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

@raviteja is this still not working for you? If so, we would need some more information or examples of this file format. But as you accepted this answer, I would assume it solved your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.