Java integer to byte array Java integer to byte array arrays arrays

Java integer to byte array


using Java NIO's ByteBuffer is very simple:

byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();for (byte b : bytes) {   System.out.format("0x%x ", b);}

output:

0x65 0x10 0xf3 0x29 


How about:

public static final byte[] intToByteArray(int value) {    return new byte[] {            (byte)(value >>> 24),            (byte)(value >>> 16),            (byte)(value >>> 8),            (byte)value};}

The idea is not mine. I've taken it from some post on dzone.com.


BigInteger.valueOf(1695609641).toByteArray()