Java Utililty Methods Integer to Byte Array

List of utility methods to do Integer to Byte Array

Description

The list of methods to do Integer to Byte Array are organized into topic(s).

Method

byte[]intToBytes(int ipInt)
ipInt -> byte[]
byte[] ipAddr = new byte[INADDRSZ];
ipAddr[0] = (byte) ((ipInt >>> 24) & 0xFF);
ipAddr[1] = (byte) ((ipInt >>> 16) & 0xFF);
ipAddr[2] = (byte) ((ipInt >>> 8) & 0xFF);
ipAddr[3] = (byte) (ipInt & 0xFF);
return ipAddr;
byte[]intToBytes(int n)
int To Bytes
return intToBytes(n, new byte[4], 0);
byte[]intToBytes(int n)
Convert a integer value to bytes
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
    b[i] = (byte) (n >> (24 - i * 8));
return b;
voidintToBytes(int num, byte[] arr, int pos)
Writes an integer to 4 bytes in big-endian notation to the byte array.
checkBounds(arr, pos);
arr[pos] = (byte) ((num & 0xFF000000) >> 24);
arr[pos + 1] = (byte) ((num & 0x00FF0000) >> 16);
arr[pos + 2] = (byte) ((num & 0x0000FF00) >> 8);
arr[pos + 3] = (byte) ((num & 0x000000FF));
intintToBytes(int num, byte[] bytes, int startIndex)
translate int into bytes, stored in byte array starting from startIndex
bytes[startIndex] = (byte) (num & 0xff);
bytes[startIndex + 1] = (byte) ((num >> 8) & 0xff);
bytes[startIndex + 2] = (byte) ((num >> 16) & 0xff);
bytes[startIndex + 3] = (byte) ((num >> 24) & 0xff);
return startIndex + 4;
voidintToBytes(int number, byte[] destination, int destinationIndex)
Convert an Integer to four bytes
if (destination == null) {
    return;
int idx = destinationIndex;
final int MASK = 0x000000ff;
destination[idx++] = (byte) ((number >>> 24) & MASK);
destination[idx++] = (byte) ((number >>> 16) & MASK);
destination[idx++] = (byte) ((number >>> 8) & MASK);
...
byte[]intToBytes(int v)
Converts an integer to a byte array Byte order: big endian
byte[] b = new byte[4];
b[0] = (byte) ((v >>> 24) & BYTESIZE);
b[1] = (byte) ((v >>> 16) & BYTESIZE);
b[2] = (byte) ((v >>> 8) & BYTESIZE);
b[3] = (byte) ((v) & BYTESIZE);
return b;
byte[]intToBytes(int v)
int To Bytes
byte[] bytes = new byte[4];
bytes[0] = (byte) (v >>> 24);
bytes[1] = (byte) (v >>> 16);
bytes[2] = (byte) (v >>> 8);
bytes[3] = (byte) (v >>> 0);
return bytes;
voidintToBytes(int v, byte[] bytes)
int To Bytes
intToBytes(v, bytes, 0);
byte[]intToBytes(int v, final byte[] arr)
Returns a Little-Endian byte array extracted from the given int.
for (int i = 0; i < 4; i++) {
    arr[i] = (byte) (v & 0XFF);
    v >>>= 8;
return arr;