Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I convert a big-endian(64 bit) to little endian(64 bit) in Java

 
Sukhpreet Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How do I convert a big-endian(64 bit) to little endian(64 bit) in Java and vice versa.

For e.g. I have 40429C28F5C28F5C in big endian, I want to convert it into little endian (5C8FC2F5289C4240) and vice versa.


How would I code a function to do this in Java?

Thanks.

Sukhpreet Kaur
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out EndianUtils, part of Apache Commons IO
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need third party tools for this -- the java.nio.ByteBuffer class can do this for you.

Just allocate a byte buffer of at least 8 bytes, set the order to big endian, put in the long value, set the order to little endian, reset the position to zero, and get the long value back from the buffer.

Henry
 
Sukhpreet Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

I tried to read through and use the java.nio.ByteBuffer class , but I am little confused as I am new to Java.

Can you please explain me with an example.

Thank you.

Regards,
Sukhpreet Kaur
 
Sukhpreet Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

I have tried to follow the steps you have mentioned :

// Create an empty ByteBuffer with a 8 byte capacity
ByteBuffer bbuf = ByteBuffer.allocate(8);

//Set the order to Big Endian
bbuf.order(ByteOrder.BIG_ENDIAN) ;

// Use the putLong() for putting in the Long Value.
bbuf.putLong((long) 37.22);

//Set the order to Little Endian
bbuf.order(ByteOrder.LITTLE_ENDIAN) ;

// Get the Long value back
ByteBuffer b1 = bbuf.getLong((long) 37.22 );

Its giving some error in conversion from Long to Byte....

Any suggestions.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Henry was assuming (as was I) that you'd be using a long value. Instead it looks like you're using a double. In that case, don't use putLong() and getLong() - use putDouble() and getDouble(). Also, the call to getLong() or getDouble() shouldn't have 37.22 passed in as an argument. You've already used that number; it's been put into the buffer. Now you're trying to get the reversed value out. If you do supply an argument, it should be the position in the buffer that you're reading from - which is the beginning, or position 0. One way or another you need to tell the buffer that you're going back to the beginning to read - there are several ways, but simply caling getDouble(0) works well:

Or equivalently:

Since most of the ByteBuffer methods return the ByteBuffer itself as the return value, you can chain everything together if you like. This style may look a little weird to some people though. It's your call.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally the same method will work equally well whether you're converting from big endian to little endian or vice versa. The result is the same in both cases. So don't be put off that it looks like it's only converting big to little. You can experiment and verify that you'd get the same results converting little to big.
 
Sukhpreet Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

Thank you so much for your help ....

Yes I used zero as an argument to bbuf.getLong() but it wasn't giving me the required result so I tried 37.22 to see what does it do ....


I have tried the piece of code you have posted (with an argument as 37.22) and it is giving me the following answer :

0x7.3873686078661125E137

But I am looking for something like this ....


By using
Long.toHexString(Double.doubleToLongBits(37.22))
I get the result as:
(40429C28F5C28F5C)
which is in Big Endian and I want to convert it into Little Endian so that I can use this value.


1.)What I need is if I enter (40429C28F5C28F5C) as an argument to the function, it should convert it to little endian (5C8FC2F5289C4240) ....

I have found the following example (but for 4-bytes):

http://www.rgagnon.com/javadetails/java-0007.html


I am looking for something like that for 8-bytes(64 bits)?


Thank you.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.)What I need is if I enter (40429C28F5C28F5C) as an argument to the function, it should convert it to little endian (5C8FC2F5289C4240) ....




Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have tried the piece of code you have posted (with an argument as 37.22) and it is giving me the following answer :

0x7.3873686078661125E137

But I am looking for something like this ....


Believe it or not, the answer is correct. The bytes are being converted to little endian. It is what was printed out that is not what you expected -- but the values are correct.

If you had did a double to long bits of the result, and then converted it to a hex string. You would have seen that it is the correct value.

Henry
 
Sukhpreet Kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thank you so much ...
it works ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic