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(Number[] array)
Turns the Number array into one consisting of primitive bytes.
byte[] result;
int i;
result = new byte[array.length];
for (i = 0; i < array.length; i++)
    result[i] = array[i].byteValue();
return result;
byte[]toByteArray(Object obj)
to Byte Array
if (obj instanceof Object[]) {
    Object[] arr = (Object[]) obj;
    byte[] byteArr = new byte[arr.length];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] instanceof Integer) {
            byteArr[i] = ((Integer) arr[i]).byteValue();
        } else if (arr[i] instanceof Double) {
            byteArr[i] = ((Double) arr[i]).byteValue();
...
byte[]toByteArray(Object value)
to Byte Array
if (value == null) {
    return null;
} else if (value instanceof byte[]) {
    return (byte[]) value;
} else {
    throw new IllegalArgumentException();
byte[]toByteArray(short foo)
Description of the Method
return toByteArray(foo, new byte[2]);
byte[]toByteArray(String address)
to Byte Array
byte[] result = new byte[4];
String[] strings = address.split("\\.");
for (int i = 0, n = strings.length; i < n; i++) {
    result[i] = (byte) Integer.parseInt(strings[i]);
return result;
byte[]toByteArray(String binString)
to Byte Array
if (binString == null) {
    return null;
binString = binString.replaceAll(" ", "");
if (binString.isEmpty()) {
    return new byte[0];
int length = binString.length();
...
byte[]toByteArray(String bytesStr, int byteLength)
to Byte Array
if (bytesStr == null) {
    return new byte[byteLength];
if (bytesStr.length() != (byteLength * 2)) {
    return null;
final byte[] result = new byte[byteLength];
for (int index = 0; index < byteLength; index++) {
...
byte[]toByteArray(String content, String charsetName)
to Byte Array
byte[] bytes;
try {
    bytes = content.getBytes(charsetName);
} catch (Exception ex) { 
    bytes = content.getBytes();
return bytes;
byte[]toByteArray(String hex)
to Byte Array
int len = (hex.length() + 1) / 3;
byte[] data = new byte[len];
for (int i = 0; i < len; i++) {
    data[i] = (byte) ((Character.digit(hex.charAt(i * 3), 16) << 4)
            + Character.digit(hex.charAt(i * 3 + 1), 16));
return data;
byte[]toByteArray(String hex)
Returns a byte array from a String containing hex encoding bytes.
byte[] bytes = null;
if (hex != null) {
    bytes = new byte[hex.length() / 2];
    for (int i = 0; i < hex.length(); i += 2) {
        char ch1 = hex.charAt(i);
        char ch2 = hex.charAt(i + 1);
        bytes[i / 2] = (byte) hexCharsToByte(ch1, ch2);
return bytes;