Java tutorial
//package com.java2s; public class Main { public static byte[] intToByteArray(final int integer) { int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer : integer)) / 8; byte[] byteArray = new byte[4]; for (int n = 0; n < byteNum; n++) byteArray[3 - n] = (byte) (integer >>> (n * 8)); return (byteArray); } public static byte[] intToByteArray(final int integer, byte[] input, int offset) { int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer : integer)) / 8; for (int n = 0; n < byteNum; n++) input[offset + 3 - n] = (byte) (integer >>> (n * 8)); return input; } }