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 val)
int To Bytes
if (val == 0)
    return EMPTY_BYTE_ARRAY;
int lenght = 0;
int tmpVal = val;
while (tmpVal != 0) {
    tmpVal = tmpVal >> 8;
    ++lenght;
byte[] result = new byte[lenght];
int index = result.length - 1;
while (val != 0) {
    result[index] = (byte) (val & 0xFF);
    val = val >> 8;
    index -= 1;
return result;
voidintToBytes(int val, byte[] bytes, int start)
int To Bytes
bytes[start + 0] = (byte) val;
bytes[start + 1] = (byte) (0xff & (val >> 8));
bytes[start + 2] = (byte) (0xff & (val >> 16));
bytes[start + 3] = (byte) (0xff & (val >> 24));
byte[]intToBytes(int val, int byteCount)
int To Bytes
byte[] buffer = new byte[byteCount];
int[] ints = new int[byteCount];
for (int i = 0; i < byteCount; i++) {
    ints[i] = val & 0xFF;
    buffer[byteCount - i - 1] = (byte) ints[i];
    val = val >> 8;
    if (val == 0) {
        break;
...
byte[]intToBytes(int value)
Converts an int into an array of 4 bytes.
byte[] out = new byte[4];
out[0] = (byte) (value >> 0);
out[1] = (byte) (value >> 8);
out[2] = (byte) (value >> 16);
out[3] = (byte) (value >> 24);
return out;
byte[]intToBytes(int value)
int To Bytes
int size = 4;
byte[] bytes = new byte[size];
for (int i = 0; i < size; i++) {
    int mask = 0xFF << (8 * i);
    bytes[size - 1 - i] = (byte) ((value & mask) >> (8 * i));
return bytes;
byte[]intToBytes(int value)
Converting int to bytes
return new byte[] { (byte) (value & 0xFF), (byte) ((value >> 8) & 0xFF), (byte) ((value >> 16) & 0xFF),
        (byte) ((value >> 24) & 0xFF) };
voidintToBytes(int value, byte[] array, int offset)
Marshal an integer to a byte array.
array[offset++] = (byte) ((value >>> 24) & 0xFF);
array[offset++] = (byte) ((value >>> 16) & 0xFF);
array[offset++] = (byte) ((value >>> 8) & 0xFF);
array[offset++] = (byte) ((value >>> 0) & 0xFF);
intintToBytes(int value, byte[] buffer, int offset)
Writes an int into a buffer.
buffer[offset + 3] = (byte) (value & 0xff);
value = value >> 8;
buffer[offset + 2] = (byte) (value & 0xff);
value = value >> 8;
buffer[offset + 1] = (byte) (value & 0xff);
value = value >> 8;
buffer[offset] = (byte) (value);
return offset + 4;
...
voidintToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian)
Converts an int to a sequence of bytes of the specified length and with the specified byte order.
if (littleEndian)
    intToBytesLE(value, buffer, offset, length);
else
    intToBytesBE(value, buffer, offset, length);
voidintToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
Converts a 16 bit sample of type int to 2 bytes in an array.
if (bigEndian) {
    buffer[byteOffset++] = (byte) (sample >> 8);
    buffer[byteOffset] = (byte) (sample & 0xFF);
} else {
    buffer[byteOffset++] = (byte) (sample & 0xFF);
    buffer[byteOffset] = (byte) (sample >> 8);