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
To do this, we shift the rightmost 8 bits by 24 to the left so that it becomes the leftmost 8 bits. We left shift the right middle byte by 16 (to store it as the left middle byte) We left shift the left middle byte by 8 (to store it as the right muddle byte) We finally left shift the leftmost byte by 24 to the left.
Big-endian is an order in which the "big end" (most significant value in the sequence) is stored first, at the lowest storage address. Little-endian is an order in which the "little end" (least significant value in the sequence) is stored first.
PNG files store 16-bit pixels in network byte order (big-endian, ie most significant bytes first).
The advantages of Big Endian and Little Endian in a computer architecture. According to Wiki, Big endian is “the most common format in data networking”, many network protocols like TCP, UPD, IPv4 and IPv6 are using Big endian order to transmit data. Little endian is mainly using on microprocessors.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With