Now I have a program in which I have to read numbers from binary file.The numbers are in little endian and I have to convert into big endian for java code.I am not getting anything.So anyone can post how can I do it.please.Thank you.
-
See also this [Q&A](http://stackoverflow.com/q/2211927/230513). – trashgod Feb 12 '13 at 00:18
5 Answers
Good answers so far but I have to admit however, that these days I spend more time in .NET than I do in java.
However, I did feel that as well as the answers others have put on here, it might actually be worth posting an answer describing just what the difference is between little endian and big endian.
Often, when I see questions like this, people don't realise that it's actually a very simple task that will take you no more than about 5 minutes to implement.
Endiness is all about the order the bytes for a given number are actually stored in a binary file.
Let's take a simple example of a 16 bit "short integer" (the smallest size that can be effected by this)
If you stop to think about this in terms of "bytes" then you immediately see that 16 bits actually equals 2 bytes.
now, if you start breaking those bytes down into lowest and highest orders you actually get a low order byte and a high order byte.
if we imagine, we have the value 511, that is represented by 2 bytes of
1
and
256
why?
Bit positions in numbers are powers of 2, if you go across the bits in powers of two from right to left you get:
128 64 32 16 8 4 2 1
if you add all of these columns together, you'll see the maximum value you can get is 255
when all the bits are full, you have to move into the next bit, so in order to get 511 we have to have
256 128 64 32 16 8 4 2 1
which by extension of splitting into bytes really becomes
1 | 128 64 32 16 8 4 2 1
with the | character representing the split between two 8 bit bytes.
or in graphical form
-------------
| 1 | 255 |
-------------
I'm not going to go too deep into Binary, otherwise I'll end up going off topic, but there are plenty of decent references out there such as this one on Wikipedia:
http://en.wikipedia.org/wiki/Binary_number
back to our bytes...
when you write those bytes to a file, you can either write the 1 then 255 or you can write the 255 then the 1
if you write the 1 first, then that's known as "Big Endian" because your writing the biggest (highest) value of the two bytes first.
If you write the 255 first, then your writing the smallest (lower) of the two values first, and so is given the name "Little Endian"
How do you convert from one to the other?
It's quite easy really, if the values are stored in the file as a "Little Endian" value, then you essentially need to do the following:
Read One Byte into A
Read One Byte into B
Make 16 bit Result = (B << 8) + A
if it's stored in the file as a "Big Endian" Value
Read One Byte into A
Read One Byte into B
Make 16 bit Result = (A << 8) + B
Other number types are just as simple, take a regular 32 bit integer...
Break it down it's equal to 4 , 8 bit bytes
-----------------
| 4 | 3 | 2 | 1 |
-----------------
with 1 being the lowest, and 4 being the highest.
In "Little Endian" the bytes would be stored in the file as follows:
-----------------
| 1 | 2 | 3 | 4 |
-----------------
to read it back out:
Read One Byte into A
Read One Byte into B
Read One Byte into C
Read One Byte into D
Make 32 bit Result = (D << 24) + (C << 16) + (B << 8) + A
and in "Big Endian" :
-----------------
| 4 | 3 | 2 | 1 |
-----------------
to read it back out:
Read One Byte into A
Read One Byte into B
Read One Byte into C
Read One Byte into D
Make 32 bit Result = (A << 24) + (B << 16) + (C << 8) + D
so you see, as long as you know how to read individual bytes (In any language) you really don't need any extra routines or library calls, a little bit of left shifting is all you need...
and for those that are curious:
to write a 32 bit integer as
"Little Endian":
Make R = 32 bit integer to store
Make 8 bit Value A = R AND 255
Make 8 bit Value B = (R >> 8) AND 255
Make 8 bit Value C = (R >> 16) AND 255
Make 8 bit Value D = (R >> 24) AND 255
Write A as byte to file
Write B as byte to file
Write C as byte to file
Write D as byte to file
"Big Endian":
Make R = 32 bit integer to store
Make 8 bit Value A = R AND 255
Make 8 bit Value B = (R >> 8) AND 255
Make 8 bit Value C = (R >> 16) AND 255
Make 8 bit Value D = (R >> 24) AND 255
Write D as byte to file
Write C as byte to file
Write B as byte to file
Write A as byte to file

- 5,729
- 2
- 37
- 71
You might be doing something wrong in your code. I wrote a blog post recently on how to read and write binary files and how to convert endianness. You need to call flip() after reading in the data to byte
FileChannel fc = (FileChannel) Files.newByteChannel(Paths.get(filename), StandardOpenOption.READ);
ByteBuffer byteBuffer = ByteBuffer.allocate((int)fc.size());
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
fc.read(byteBuffer);
byteBuffer.flip();
http://pulasthisupun.blogspot.com/2016/06/reading-and-writing-binary-files-in.html

- 1,730
- 1
- 17
- 29