I am trying to read a binary file from a program that writes a log (of sorts) to a dat file which I have worked out reasonably well the format of using Java. I am loading it as so:
DataInputStream in = new DataInputStream(new FileInputStream("file.dat"));
System.out.println("Bytes skipped: " + in.skipBytes(4));
System.out.println(in.readLong());
The problem is the value from readLong() is different to what I am expecting, in Hex Workshop I highlight the hex blocks
BF02 0000
and reports that it is a valid signed short/long number – however the output is very different to what I am expecting. Looking at the Java Docs it states that it classes a long as 64 bit (8 bytes) whereas other sources show a signed long integer should be 32 bits – is there a way to get around this?
Cheers,
Tom
primitive types means different things in different languages and different platforms. (e.g. In C, it’s not uncommon that a a long is 32 bit on some platforms and 64bits on others.
First you have to know the type in your .dat file, and the byte order (big/little endian).
Then assemble the individual bytes into an appropriate java type.
If the .dat files specifies a 32 bit signed integer, an int in java would be suitable.
If it was an unsigned 32 bit integer, you probably would need to use a long in java to capture all the possible values, since java does not have unsigned types.
Read it as follows if the integer in the file is little endian:
and if it’s big endian, do
(I don’t remember atm. the promotion rules in java here, you might need to and them with &0xff to produce an int before the bit shifting)
Of course, you can read in a byte array and operate on that array instead of calling in.readByte() individually if you want.