This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sirisena,

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

 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Duplicate removed. Please do not post the same question more than once.]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic