aspose file tools*
The moose likes Java in General and the fly likes Using ByteBuffer for Converting Byte Order from BigEndian to Little Endian. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using ByteBuffer for Converting Byte Order from BigEndian to Little Endian." Watch "Using ByteBuffer for Converting Byte Order from BigEndian to Little Endian." New topic
Author

Using ByteBuffer for Converting Byte Order from BigEndian to Little Endian.

KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Hi,

I have the following requirement. I will be receiving data from a legacy system which uses Little endian byte ordering. Hence the data will be in little endian order. I am trying to use a ByteBuffer of the Java.nio to convert the data from little endian to Big endian. For Instance let me say that I have a byte array [0x80,0x01] and lets say this is in little endian, is it possible to use the Byte Buffer to convert the byte array to [0x01, 0x80]. What do i need to do when I receive the bytes from the legacy system in little endian order.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Just taking a shot in the dark...

Since the default byte ordering is Big Endian for a ByteBuffer how about calling order(ByteOrder.LITTLE_ENDIAN) on the ByteBuffer reference and start reading your bytes?
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Hi Sirisena,

I tried that too, but it does not seem to work.

Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Byte order is only meaningful when working with data types larger than 1 byte. Setting byte order doesn't change the order of bytes inside ByteBuffer, it changes only conversion to and from longer data types.

Try this code:
Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
[Duplicate removed. Please do not post the same question more than once.]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using ByteBuffer for Converting Byte Order from BigEndian to Little Endian.