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[]newByteArray(byte[] data, int finalSize)
Generates a new byte array of the given size using the given data and filling with ASCII zeros (0x48) the remaining space.
if (data == null)
    throw new NullPointerException("Data cannot be null.");
if (finalSize < 0)
    throw new IllegalArgumentException("Final size must be equal or greater than 0.");
if (finalSize == 0)
    return new byte[0];
byte[] filledArray = new byte[finalSize];
int diff = finalSize - data.length;
...
byte[]newByteArray(int len)
new Byte Array
return new byte[len];
byte[]newByteArray(int size)
Generates a byte[] the is filled with a repeated sequence of 0x00 to 0xFF.
byte[] data = new byte[size];
for (int ii = 0; ii < data.length; ii++)
    data[ii] = (byte) (ii % 256);
return data;
byte[]newByteArray(int... bytes)
new Byte Array
byte[] result = new byte[bytes.length];
int ind = 0;
for (int i : bytes) {
    result[ind++] = (byte) i;
return result;
byte[]newByteArray(int... bytes)
new Byte Array
byte[] result = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
    if (bytes[i] > 255 || bytes[i] < 0) {
        throw new IllegalArgumentException("all values must be 0 <= value < 256");
    result[i] = (byte) bytes[i];
return result;
...
bytetoByte(char[] bytes, boolean le)
to Byte
if (bytes.length != 2)
    return 0;
return (byte) Long.parseLong(bytesToString(bytes, le, true), 16);
byte[]toByteArr(final float value, final boolean lsbIsFirst)
Converts a float to byte[4].
return toByteArr(Float.floatToIntBits(value), false);
byte[]toByteArray(boolean[] array)
Coverts given booleans array to array of bytes.
byte[] result = new byte[array.length];
for (int i = 0; i < array.length; i++) {
    result[i] = array[i] ? (byte) 1 : (byte) 0;
return result;
byte[]toByteArray(boolean[] bits)
to Byte Array
byte[] dst = new byte[(int) Math.ceil((double) bits.length / 8)];
for (int i = 0; i < bits.length; i++) {
    dst[i / 8] |= (byte) ((bits[i] ? 1 : 0) << (i % 8));
return dst;
byte[]toByteArray(byte arg, int length)
to Byte Array
byte[] ret = new byte[length];
byte num = arg;
for (int i = length - 1; i >= 0; i--) {
    ret[i] = (byte) (num & 0xFF);
    num >>= 8;
return ret;