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(double[][] array)
to Byte Array
int nr = array.length;
int nc = array[0].length;
byte[][] ret = new byte[nr][nc];
for (int i = 0; i < nr; i++) {
    for (int j = 0; j < nc; j++) {
        ret[i][j] = (byte) array[i][j];
return ret;
Byte[]toByteArray(final byte[] bytes)
Produces an array of Byte objects from an array of bytes.
final Byte[] result = new Byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
    result[i] = Byte.valueOf(bytes[i]);
return result;
Byte[][]toByteArray(final byte[][] array)
Converts a double byte array into a double Byte array.
if (array == null) {
    return null;
final Byte[][] byteArray = new Byte[array.length][];
for (int i = 0; i < array.length; i++) {
    byteArray[i] = new Byte[array[i].length];
    for (int j = 0; j < array[i].length; j++) {
        byteArray[i][j] = array[i][j];
...
byte[]toByteArray(final char[] array)
Converts array of char to byte array.
final byte[] result = new byte[array.length * 2];
for (int i = 0; i < array.length; i++) {
    result[2 * i] = (byte) ((array[i] & 0xFF00) >> 8);
    result[2 * i + 1] = (byte) (array[i] & 0x00FF);
return result;
byte[]toByteArray(final char[] array)
to Byte Array
return toByteArray(array, 0, array.length);
byte[]toByteArray(final int i)
Returns the bytes (big-endian) of an integer.
final byte[] b = new byte[4];
b[0] = (byte) ((i & 0xff000000) >>> 24);
b[1] = (byte) ((i & 0x00ff0000) >>> 16);
b[2] = (byte) ((i & 0x0000ff00) >>> 8);
b[3] = (byte) ((i & 0x000000ff));
return b;
byte[]toByteArray(final int i)
Converts an int to a byte[4] array.
return toByteArray(i, BIG_ENDIAN);
voidtoByteArray(final int i, final byte[] output, final int offset)
Converts an integer to a byte array.
final int firstByteOffset = 0;
final int secondByteOffset = 1;
final int thirdByteOffset = 2;
final int fourthByteOffset = 3;
output[offset + firstByteOffset] = (byte) (i & INT_LOW_BYTE_MASK);
output[offset + secondByteOffset] = (byte) ((i >> secondByteOffset * BIT_SIZE_OF_BYTE) & INT_LOW_BYTE_MASK);
output[offset + thirdByteOffset] = (byte) ((i >> thirdByteOffset * BIT_SIZE_OF_BYTE) & INT_LOW_BYTE_MASK);
output[offset + fourthByteOffset] = (byte) ((i >> fourthByteOffset * BIT_SIZE_OF_BYTE) & INT_LOW_BYTE_MASK);
...
byte[]toByteArray(final int value)
Method used to convert integer to byet array
return new byte[] { 
        (byte) (value >> 24), 
        (byte) (value >> 16), 
        (byte) (value >> 8), 
        (byte) value 
};
byte[]toByteArray(final String bitString)
Convert a bit string into a byte array.
final byte[] array = new byte[(bitString.length() + Byte.SIZE - 1) / Byte.SIZE];
int byteNum = 0;
final int firstBits = bitString.length() % Byte.SIZE;
int pos = 0;
if (firstBits != 0) {
    array[byteNum++] = toByte(bitString.substring(0, firstBits));
    pos = firstBits;
while (byteNum < array.length) {
    array[byteNum++] = toByte(bitString.substring(pos, pos + Byte.SIZE));
    pos += Byte.SIZE;
return array;