sajjoo
sajjoo

Reputation: 6614

convert little Endian file into big Endian

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 reads in big endian format.i also had a look on ByteBuffer class but have no idea how to use it to get my desired result. please help.

thanks alot

Upvotes: 10

Views: 11022

Answers (6)

pz64_
pz64_

Reputation: 2252

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));
}

Upvotes: 2

pulasthi
pulasthi

Reputation: 1750

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

Upvotes: 0

bobah
bobah

Reputation: 18864

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).

Upvotes: 14

sajjoo
sajjoo

Reputation: 6614

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

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383716

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.

See also

Upvotes: 4

jvdneste
jvdneste

Reputation: 1677

I guess you should read every 4 bytes and simply reverse their order.

Upvotes: 0

Related Questions