This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes How do I convert a big-endian(64 bit) to little endian(64 bit) in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I convert a big-endian(64 bit) to little endian(64 bit) in Java" Watch "How do I convert a big-endian(64 bit) to little endian(64 bit) in Java" New topic
Author

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

Sukhpreet Kaur
Greenhorn

Joined: Jan 11, 2008
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19679
    
  18

Check out EndianUtils, part of Apache Commons IO


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sukhpreet Kaur
Greenhorn

Joined: Jan 11, 2008
Posts: 8
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

Joined: Jan 11, 2008
Posts: 8
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jan 11, 2008
Posts: 8
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
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40


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

Joined: Jan 11, 2008
Posts: 8
Hi Henry,

Thank you so much ...
it works ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I convert a big-endian(64 bit) to little endian(64 bit) in Java