I have to read data from a binary file in byte, int and long data sizes. I read it with RandomAccessFile
's methods readInt
, readLong
, readByte
. The problem is that the endiannes of the system
(Windows 8.1) and the endiannes of the file are different. For example this number in the file is actually the number
2
, but it is read from the system as 33554432
.
How can I fix this problem and be able to read data chunks with RandomAccessFile
's methods ?
the problem is that the endianness of the system ... and the endianness of the file are different.
No. They are the same. Look at the data. The least significant byte is at the start. That is little-endian, which is the Intel endianness. (Not the 'Windows endianness', which AFAIK doesn't exist except w.r.t. a specific platform).
The problem is that the endianness-ness of the file and the system are little-endian, but RandomAccessFile
is big-endian.
The way to address this in Java is via NIO and ByteBuffer
, using native byte order instead of the default.