• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sirisena,

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

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Duplicate removed. Please do not post the same question more than once.]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic