Android Utililty Methods Int to Byte Array Convert

List of utility methods to do Int to Byte Array Convert

Description

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

Method

byte[]IntToBytes(int value)
Int To Bytes
byte[] result = new byte[4];
result[0] = (byte) (value >> 24);
result[1] = (byte) ((value << 8) >> 24);
result[2] = (byte) ((value << 16) >> 24);
result[3] = (byte) ((value << 24) >> 24);
return result;
voidintToBytes(byte[] bytes, int offset, int value)
int To Bytes
bytes[offset++] = (byte) ((value << 24) >> 24);
bytes[offset++] = (byte) ((value << 16) >> 24);
bytes[offset++] = (byte) ((value << 8) >> 24);
bytes[offset++] = (byte) (value >> 24);
byte[]intToBytes(int number)
int To Bytes
return new byte[] { (byte) ((number >> 24) & 0xFF),
        (byte) ((number >> 16) & 0xFF),
        (byte) ((number >> 8) & 0xFF), (byte) (number & 0xFF) };
byte[]intToBytes(int number)
int To Bytes
return new byte[] { (byte) ((number >> 24) & 0xFF),
        (byte) ((number >> 16) & 0xFF),
        (byte) ((number >> 8) & 0xFF), (byte) (number & 0xFF) };
byte[]toByteArray(int in)
to Byte Array
byte[] out = new byte[4];
out[0] = (byte) in;
out[1] = (byte) (in >> 8);
out[2] = (byte) (in >> 16);
out[3] = (byte) (in >> 24);
return out;
byte[]toByteArray(int in, int outSize)
to Byte Array
byte[] out = new byte[outSize];
byte[] intArray = toByteArray(in);
for (int i = 0; i < intArray.length && i < outSize; i++) {
    out[i] = intArray[i];
return out;
byte[]getTwoBytes(int i)
Gets a two byte array from an integer
byte[] bytes = new byte[2];
bytes[0] = (byte) (i & 0xff);
bytes[1] = (byte) ((i & 0xff00) >> 8);
return bytes;
voidgetTwoBytes(int i, byte[] target, int pos)
Converts an integer into two bytes, and places it in the array at the specified position
target[pos] = (byte) (i & 0xff);
target[pos + 1] = (byte) ((i & 0xff00) >> 8);
byte[]getFourBytes(int i)
Gets a four byte array from an integer
byte[] bytes = new byte[4];
int i1 = i & 0xffff;
int i2 = (i & 0xffff0000) >> 16;
getTwoBytes(i1, bytes, 0);
getTwoBytes(i2, bytes, 2);
return bytes;
voidgetFourBytes(int i, byte[] target, int pos)
Converts an integer into four bytes, and places it in the array at the specified position
byte[] bytes = getFourBytes(i);
target[pos] = bytes[0];
target[pos + 1] = bytes[1];
target[pos + 2] = bytes[2];
target[pos + 3] = bytes[3];