convert little Endian file into big Endian

Opening NIO FileChannel:

FileInputStream fs = new FileInputStream("myfile.bin");
FileChannel fc = fs.getChannel();

Setting ByteBuffer endianness (used by [get|put]Int(), [get|put]Long(), [get|put]Short(), [get|put]Double())

ByteBuffer buf = ByteBuffer.allocate(0x10000);
buf.order(ByteOrder.LITTLE_ENDIAN); // or ByteOrder.BIG_ENDIAN

Reading from FileChannel to ByteBuffer

fc.read(buf);
buf.flip();
// here you take data from the buffer by either of getShort(), getInt(), getLong(), getDouble(), or get(byte[], offset, len)
buf.compact();

To correctly handle endianness of the input you need to know exactly what is stored in the file and in what order (so called protocol or format).

Leave a Comment