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.
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
posted
0
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: 18670
posted
0
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: 18670
posted
0
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
posted
0
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):
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.