Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Question on converting an int to 4 byte array

807580Feb 26 2010 — edited Feb 26 2010
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:
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2010
Added on Feb 26 2010
3 comments
175 views