Java integer to byte array
Solution 1:
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
Solution 2:
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.