Java Utililty Methods Byte Array Create

List of utility methods to do Byte Array Create

Description

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

Method

byte[]toByteArray(int value)
Converts an int into a 4-byte array in Big Endian order (most-significant byte in slot 0)
byte[] output = new byte[4];
toByteArray(value, output);
return output;
byte[]toByteArray(int value)
to Byte Array
return new byte[] { (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value };
voidtoByteArray(int value, int numBytes, byte[] dest, int off)
Converts a signed int into an array of bytes and writes it to a byte array t the given offset.
for (int i = 0; i < numBytes; i++) {
    dest[i + off] = (byte) (value >>> ((i + 2) * 8) & 0xff);
byte[]toByteArray(int[] array)
to Byte Array
byte[] data = new byte[array.length];
for (int i = 0; i < array.length; i++) {
    data[i] = (byte) array[i];
return data;
byte[]toByteArray(int[] data)
Convenience method to transform an int[] array to a byte array for serialization.
byte[] tmp, result = new byte[data.length * 4];
for (int i = 0; i < data.length; i++) {
    tmp = toBytes(data[i]);
    System.arraycopy(tmp, 0, result, i * 4, 4);
return result;
byte[]toByteArray(int[] data, boolean includeLength)
to Byte Array
int n = data.length << 2;
if (includeLength) {
    int m = data[data.length - 1];
    n -= 4;
    if ((m < n - 3) || (m > n)) {
        return null;
    n = m;
...
byte[]toByteArray(long hi, long lo)
converts two longs to a byte[]
byte[] bytes = new byte[16];
int count = 0;
int v;
while (count < 16) {
    if (count == 0) {
        v = (int) (hi >>> 32);
    } else if (count == 4) {
        v = (int) hi;
...
byte[]toByteArray(Long mac)
to Byte Array
byte macArr[] = new byte[] { (byte) ((mac >> 40) & 0xff), (byte) ((mac >> 32) & 0xff),
        (byte) ((mac >> 24) & 0xff), (byte) ((mac >> 16) & 0xff), (byte) ((mac >> 8) & 0xff),
        (byte) ((mac >> 0) & 0xff), };
return macArr;
byte[]toByteArray(long value)
to Byte Array
byte[] b = new byte[Long.BYTES];
for (int i = 0; i < b.length; ++i) {
    b[i] = (byte) (value >> (Long.BYTES - i - 1 << 3));
return b;
byte[]toByteArray(long value)
to Byte Array
byte[] b = new byte[8];
for (int i = 0; i < 8; i++) {
    int offset = (8 - 1 - i) * 8;
    b[i] = (byte) (value >>> offset & 0xFF);
return b;