Results 1 to 3 of 3
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Question How do I convert Little endian to Big endian?

    Hello,

    I am reading a file a byte at a time and storing it in a Byte array.
    There is one point in the file where I suspect the bytes are being stored in little endian format.
    Since java uses only Big endian (right?) how would I go about taking this Byte, extracting its little endian value doing what ever I need to do to it and then store it back as a little endian format byte?

    Thank you for any help.

    Example:

    00000010 - if i take the value in java it will be 2 i need to have java take its value as 64, then I would lets say half this value making it 32 and then store it back in little endian format so it would store as 00000100.
    A built in function would be great but I havent had any luck finding any...

  2. #2
    Kenan89 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default .

    i found this on google maybe you can use it:

    Java Code:
    
    // 2-byte number
    int SHORT_little_endian_TO_big_endian(int i)
    {
        return ((i>>8)&0xff)+((i << 8)&0xff00);
    }
    
    // 4-byte number
    int INT_little_endian_TO_big_endian(int i)
    {
        return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,449
    Blog Entries
    7
    Rep Power
    20

    Default

    Read the API documentation for the ByteBuffer class; you can create one given an array of bytes and you can set the byte order with one of its order( ... ) methods; at the end just read integer values from the buffer.

    kind regards,

    Jos

Similar Threads

  1. Anyone know .wav data points endian format?
    By Krooger in forum New To Java
    Replies: 1
    Last Post: 12-24-2010, 09:40 AM
  2. convert XML using XSL
    By rajjan4u in forum XML
    Replies: 3
    Last Post: 11-20-2009, 04:37 PM
  3. Convert to Int
    By 6kyAngel in forum Java Tip
    Replies: 0
    Last Post: 03-06-2009, 09:18 AM
  4. how to convert xml to xsd
    By adi in forum XML
    Replies: 2
    Last Post: 02-12-2009, 02:43 PM
  5. Replies: 3
    Last Post: 01-15-2008, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •