Results 1 to 6 of 6
  1. #1
    kook04 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Need help converting int to a 4 byte array

    Hello all. I apologize in advance if this information is readily available, because I haven't been able to find a definitive answer to this question. I need to convert an int to a big endian 4 byte-array. I have the following code, which I'm fairly sure works, but it's the big endian part I'm not sure about. Can anyone confirm that this code snippet will do what I expect:

    Java Code:
    public static final byte[] intToByteArray(int value)
    {
        return new byte[]
        { 
            (byte)(value & 0xff),
            (byte)(value >> 8 & 0xff),
            (byte)(value >> 16 & 0xff),
            (byte)(value >>> 24) };
        }
    }
    Thank you very much in advance for your help.

    -Kennedy

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

    Default

    Quote Originally Posted by kook04 View Post
    Hello all. I apologize in advance if this information is readily available, because I haven't been able to find a definitive answer to this question. I need to convert an int to a big endian 4 byte-array. I have the following code, which I'm fairly sure works, but it's the big endian part I'm not sure about. Can anyone confirm that this code snippet will do what I expect:

    Java Code:
    public static final byte[] intToByteArray(int value)
    {
        return new byte[]
        { 
            (byte)(value & 0xff),
            (byte)(value >> 8 & 0xff),
            (byte)(value >> 16 & 0xff),
            (byte)(value >>> 24) };
        }
    }
    Thank you very much in advance for your help.

    -Kennedy
    Swap those four lines that initialize your array. Big endian means that the highest byte of the number comes first (opposite to little endian or 'intel endian').

    kind regards,

    Jos

  3. #3
    kook04 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    Jos,

    Are you saying that I should reverse the order of the elements in the array? It sounds like you are saying that the following would be big endian, is that true?

    Java Code:
    public static final byte[] intToByteArray(int value)
    {
        return new byte[]
        { 
            (byte)(value >>> 24),
            (byte)(value >> 16 & 0xff),
            (byte)(value >> 8 & 0xff),
            (byte)(value & 0xff)
        };    
    }
    Thanks again.

    -Kennedy

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

    Default

    Quote Originally Posted by kook04 View Post
    Jos,

    Are you saying that I should reverse the order of the elements in the array? It sounds like you are saying that the following would be big endian, is that true?

    Java Code:
    public static final byte[] intToByteArray(int value)
    {
        return new byte[]
        { 
            (byte)(value >>> 24),
            (byte)(value >> 16 & 0xff),
            (byte)(value >> 8 & 0xff),
            (byte)(value & 0xff)
        };    
    }
    Yep, your first version stored the integer in little endian order and this version stores the int in big endian order in the array.

    kind regards,

    Jos

  5. #5
    kook04 is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    Jos,

    Thank you again for your replies. If you have a moment, please take a look at the following methods. The intent is that these all output data in Big Endian format. However, based on your statements above, I think that they are actually providing Little Endian data.

    Java Code:
    public static int parseUnsignedShort(byte[] data, int offset)
    {
        int returnValue = 0;
    
        returnValue = returnValue | (unsignedByteToInt(data[offset]));
        returnValue = returnValue | (unsignedByteToInt(data[offset + 1]) << 8);
    
        return StrictMath.abs(returnValue);
    }
    Java Code:
    public static int parseUnsigned3ByteInt(byte[] data, int offset)
    {
        int returnValue = 0;
    
        returnValue = returnValue | (unsignedByteToInt(data[offset + 2]) << 16);
        returnValue = returnValue | (unsignedByteToInt(data[offset + 1]) << 8);
        returnValue = returnValue | (unsignedByteToInt(data[offset + 0]) << 0);
    
        return StrictMath.abs(returnValue);
    }
    Java Code:
    public static int parseUnsigned4ByteInt(byte[] data, int offset)
    {
        int returnValue = 0;
    
        returnValue = returnValue | (unsignedByteToInt(data[offset + 3]) << 24);
        returnValue = returnValue | (unsignedByteToInt(data[offset + 2]) << 16);
        returnValue = returnValue | (unsignedByteToInt(data[offset + 1]) << 8);
        returnValue = returnValue | (unsignedByteToInt(data[offset + 0]) << 0);
    
        return StrictMath.abs(returnValue);
    }
    Thank you again, I really do appreciate your time.

    -Kennedy

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

    Default

    Quote Originally Posted by kook04 View Post
    Jos,

    Thank you again for your replies. If you have a moment, please take a look at the following methods. The intent is that these all output data in Big Endian format. However, based on your statements above, I think that they are actually providing Little Endian data.
    Yep, those methods all treat the byte arrays as low endian ints. Big endian ints as stored with their 'most important' byte at a lowest address, little endian ints are stored vice versa. You may want to look a ByteBuffers, they can fiddle diddle with the endianess of ints as well.

    kind regards,

    Jos

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-11-2009, 11:33 PM
  3. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM
  4. Converting a byte[] into Sound
    By savage82 in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 11:57 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
  •