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.
//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....
Joined: Jan 30, 2000
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:
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
Joined: Jan 30, 2000
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.
Joined: Jan 11, 2008
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 :
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):