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[]int2bytes(int i)
intbytes
byte[] bytes = new byte[4];
int2bytes(i, bytes, 0);
return bytes;
byte[]int2Bytes(int i, byte[] bytes, int offset)
int Bytes
if (bytes == null || bytes.length - offset < 4)
    throw new Exception("bytes==null||bytes.length-offset<4");
bytes[0 + offset] = (byte) (i & 0xFF);
bytes[1 + offset] = (byte) ((i >> 8) & 0xFF);
bytes[2 + offset] = (byte) ((i >> 16) & 0xFF);
bytes[3 + offset] = (byte) ((i >> 24) & 0xFF);
return bytes;
byte[]int2bytes(int integer)
Split an int into 4 bytes.
final byte[] byteStr = new byte[4];
byteStr[0] = (byte) (integer >>> 24);
byteStr[1] = (byte) ((integer >>> 16) & 0xff);
byteStr[2] = (byte) ((integer >>> 8) & 0xff);
byteStr[3] = (byte) (integer & 0xff);
return byteStr;
byte[]int2bytes(int intValue)
int to bytes
byte[] bytes = new byte[4];
bytes[0] = (byte) (intValue >> 24);
bytes[1] = (byte) ((intValue << 8) >> 24);
bytes[2] = (byte) ((intValue << 16) >> 24);
bytes[3] = (byte) ((intValue << 24) >> 24);
return bytes;
intint2Bytes(int l, byte[] target, int offset)
returns the next index
for (int i = offset + 3; i >= offset; i--) {
    target[i] = (byte) (l & 0xFF);
    l >>= 8;
return offset + 4;
voidint2Bytes(int n, byte[] dst, int start)
int Bytes
dst[start] = (byte) (n & 0xff);
dst[start + 1] = (byte) ((n & 0xff00) >> 8);
dst[start + 2] = (byte) ((n & 0xff0000) >> 16);
dst[start + 3] = (byte) ((n & 0xff000000) >> 24);
byte[]int2Bytes(int num)
Big Endian
byte[] bytes = new byte[4];
for (int i = 0; i < 4; i++) {
    bytes[3 - i] = (byte) (num >>> 8 * i);
return bytes;
byte[]int2bytes(int num)
intbytes
byte[] bytes = new byte[] { (byte) (num >> 24), (byte) (num >> 16), (byte) (num >> 8), (byte) (num) };
return bytes;
byte[]int2Bytes(int num)
Convert int to bytes, for small integer, only low-digits are stored in bytes, and all high zero digits are removed to save memory.
byte[] buffer = new byte[4];
for (int ix = 0; ix < 4; ++ix) {
    int offset = 32 - (ix + 1) * 8;
    buffer[ix] = (byte) ((num >> offset) & 0xff);
int i = 0;
for (i = 0; i < buffer.length - 1; i++) {
    byte b = buffer[i];
...
byte[]int2bytes(int num)
intbytes
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
    b[i] = (byte) (num >>> (24 - i * 8));
for (byte by : b) {
    System.out.println(by);
return b;
...