Java integer to byte array [整型转byte[]字节数组,数组长可设置]

using Java NIO's ByteBuffer is very simple:

Code1:
byte[] bytes = ByteBuffer.allocate(4).putInt(130776).array();
for (byte b : bytes) {
   System.out.format("0x%x ", b);
}
output:
0x00 0x01 0xfe 0xd8 
Code2:
public static final byte[] intToByteArray(int value) {
    return new byte[] {
            (byte)(value >>> 24),
            (byte)(value >>> 16),
            (byte)(value >>> 8),
            (byte)value};
} 
Code3:
byte[] IntToByteArray( int data ) {

    byte[] result = new byte[4];

    result[0] = (byte) ((data & 0xFF000000) >> 24);
    result[1] = (byte) ((data & 0x00FF0000) >> 16);
    result[2] = (byte) ((data & 0x0000FF00) >> 8);
    result[3] = (byte) ((data & 0x000000FF) >> 0);

    return result;
}

你可能感兴趣的:(Java integer to byte array [整型转byte[]字节数组,数组长可设置])