Re: How to read binary data



Dominik G wrote:

.... but unfortunately it does not work. I am getting 172777843 instead
of 56.

I don't know where you are getting the value 172777843 from, perhaps you are reading the wrong file, or the wrong offset in the file. What you posted up thread reads fine for me, however. To swap endian for an int, use Integer.reverseBytes(int):

<code>
DataInputStream din = new DataInputStream(
new FileInputStream( "src/fubar/file.dat") );
for( int i, x=0; (i = din.readInt()) != -1 && x < 10; x++ ) {
System.out.print( Integer.reverseBytes(i)+", ");
}
in.close();
</code>

<output>
56, 1264145485, 1313622348, 1280592711, 1413826379, 1095062612, 1094796374, 1262829908, 1363887702, 1145979225,
</output>

Note the first value, 56 ,is correct. If 56 itself is byte swapped, I get 939524096 decimal, I have no idea where your value came from. Perhaps there is a problem in code you haven't shown us....

Are the remaining values doubles? floats? I confess I'm happily Perl illiterate. Your code example did nothing for me.

.