0

Can anybody please tell me how to get double and int value from a binary file. I have tried alot but its giving me wrong values. Here is my code.

InputStream iStream = getApplicationContext().getResources().openRawResource(R.raw.map);

    DataInputStream input;

        input = new DataInputStream(iStream);   

            try {                   

double mapFileFormatVersionNumber, IntendedSoftwareVersion;         
int DemoDays;

mapFileFormatVersionNumber =input.readDouble();                     
IntendedSoftwareVersion = input.readDouble();
DemoDays = input.readInt();

thanks in advance.

11
  • Where did the file come from? What's the format? The DataInputStream docs tell you the expected format - but presumably that's not the format of your actual file... Commented Aug 5, 2010 at 12:35
  • i have made a folder named Raw in RES and put my file in it. and my file format is .AMF (Anquet Map File). Commented Aug 5, 2010 at 12:36
  • @sajjoo: Do you have a link to the specification for that file type? Does it match DataInputStream's expected format? Commented Aug 5, 2010 at 12:44
  • actually i know the specifications of that file. where are the strings, integers, double, float i know that all. and i also code it having that specs in my mind. and strings results are okay but doubles and integers giving me wrong values. Commented Aug 5, 2010 at 13:04
  • they may not be of same bit length, they may not have the same endianness, etc. etc. And you should please kindly elaborate on your problem, not want us to read previous questions. All in all, you want US to help you, not the other way around. So you should do as much as possible for us to make it easy to help you. Commented Aug 5, 2010 at 13:37

2 Answers 2

1

Tthere are a gazillion possible representations of integers and doubles. Hence you simply can't expect DataInputStream to be compatible with whatever bizarre binary format. You'll have to go through the specs of .AMF file format and roll your own conversion.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no difference in C++ and Java integer types. They are both signed 32-bit numeric types. However, its binary representation might be different depending on the byte order. For example, integer might be represented in file in little endian byte order and reading it assuming it is in big endian will result in wrong data. You might want to experiment with byte order if you don't know exactly how it is stored in the file. Take a look at ByteBuffer Java class, it supports different byte orders.

Comments

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.