how can i convert a liitle Endian binary file into big Endian binary file. i have a binary binary written in C and i am reading this file in Java with DataInputStream which
I guess you should read every 4 bytes and simply reverse their order.
after Googling so much i have found a apache Jar file which has SwappedDataInputStream Class. org.apache.commons.io.input.SwappedDataInputStream. this class made my results accurate. for full detail of that class see.
http://commons.apache.org/io/api-1.4/org/apache/commons/io/input/SwappedDataInputStream.html
I recently wrote a blog post on doing exactly this. On how you can convert binary files between endianness. Adding it here for future reference for anyone coming here.
You can get this done from the following simple code
FileChannel fc = (FileChannel) Files.newByteChannel(Paths.get(filename), StandardOpenOption.READ);
ByteBuffer byteBuffer = ByteBuffer.allocate((int)fc.size());
byteBuffer.order(ByteOrder.BIG_ENDIAN);
fc.read(byteBuffer);
byteBuffer.flip();
Buffer buffer = byteBuffer.asShortBuffer();
short[] shortArray = new short[(int)fc.size()/2];
((ShortBuffer)buffer).get(shortArray);
byteBuffer.clear();
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer shortOutputBuffer = byteBuffer.asShortBuffer();
shortOutputBuffer.put(shortArray);
FileChannel out = new FileOutputStream(outputfilename).getChannel();
out.write(byteBuffer);
out.close();
For detailed information about how this works you can refer to the original blog post - http://pulasthisupun.blogspot.com/2016/06/reading-and-writing-binary-files-in.html
Or the code is available at - https://github.com/pulasthi/binary-format-converter
The two functions below swap between the endianness of a 2 and 4 bytes.
static short Swap_16(short x) {
return (short) ((((short) (x) & 0x00ff) << 8) | (((short) (x) & 0xff00) >> 8));
}
static int Swap_32(int x) {
return ((((int) (x) & 0x000000ff) << 24)
| (((int) (x) & 0x0000ff00) << 8)
| (((int) (x) & 0x00ff0000) >> 8) | (((int) (x) & 0xff000000) >> 24));
}
You can use EndianUtils from Apache Commons I/O:
It has static
methods like long readSwappedLong(InputStream input) that can do all the swapping for you. It also has overloads that uses a byte[]
as input, as well as write
counterpart (to OutputStream
or byte[]
). It also has non-I/O methods like int swapInteger(int value) methods that can do conversion of plain Java primitives.
The package also has many useful utility classes like FilenameUtils, IOUtils, etc.
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).