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

voidintegerToBytes(int v, byte[] b)
integer To Bytes
assert b.length >= 4;
b[0] = (byte) ((v >>> 24) & 0xFF);
b[1] = (byte) ((v >>> 16) & 0xFF);
b[2] = (byte) ((v >>> 8) & 0xFF);
b[3] = (byte) ((v >>> 0) & 0xFF);
voidintegerToBytes(int v, byte[] b)
integer To Bytes
assert b.length >= 4;
b[0] = (byte) ((v >>> 24) & 0xFF);
b[1] = (byte) ((v >>> 16) & 0xFF);
b[2] = (byte) ((v >>> 8) & 0xFF);
b[3] = (byte) ((v >>> 0) & 0xFF);
byte[]integerToBytes(int value)
integer To Bytes
return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value };
byte[]integerToBytes(int x)
integer To Bytes
byte[] b = new byte[4];
for (int i = 0; i < 4; ++i) {
    b[i] = (byte) (x >> (4 - i - 1 << 3));
return b;
byte[]intTo2Bytes(int x)
int To Bytes
byte[] out = new byte[2];
out[1] = (byte) (x & 0xff);
out[0] = (byte) ((x >> 8) & 0xff);
return out;
byte[]intTo4ByteArray(int value)
Convert an integer getParamName to its byte array representation
byte[] result = new byte[4];
for (int i = 0; i < 4; i++) {
    int offset = (result.length - 1 - i) * 8;
    result[i] = (byte) ((value >>> offset) & 0xFF);
return result;
byte[]intTo4Bytes(int i)
int To Bytes
return new byte[] { (byte) ((i >> 24) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 8) & 255),
        (byte) (i & 255) };
voidintToByteArray(byte[] bytes, int i)
int To Byte Array
bytes[0] = (byte) (i >> 24);
bytes[1] = (byte) ((i << 8) >> 24);
bytes[2] = (byte) ((i << 16) >> 24);
bytes[3] = (byte) ((i << 24) >> 24);
byte[]intToByteArray(final int integer)
Returns a byte array containing the two's-complement representation of the integer.
The byte array will be in big-endian byte-order with a fixes length of 4 (the least significant byte is in the 4th element).

Example:
intToByteArray(258) will return { 0, 0, 1, 2 },
BigInteger.valueOf(258).toByteArray() returns { 1, 2 }.
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);
byte[]intToByteArray(final int integer)
int To Byte Array
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);