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[]toBytes(String hex)
to Bytes
if (hex == null || hex.length() == 0 || hex.length() % 2 == 1)
    return null;
int byteCount = hex.length() / 2;
byte[] bytes = new byte[byteCount];
for (int i = 0; i < byteCount; i++) {
    int beginIndex = i * 2;
    bytes[i] = (byte) Integer.parseInt(hex.substring(beginIndex, beginIndex + 2), 16);
return bytes;
byte[]toBytes(String hex)
Convert hex string to byte[]
if (hex == null || hex.equals("")) {
    return null;
hex = hex.toUpperCase();
int length = hex.length() / 2;
char[] hexChars = hex.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
...
byte[]toBytes(String hexStr)
to Bytes
String src = hexStr.replaceAll("\\s+", "").trim();
int len = src.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
    data[i / 2] = (byte) ((Character.digit(src.charAt(i), 16) << 4)
            + Character.digit(src.charAt(i + 1), 16));
return data;
...
byte[]toBytes(String hexStr)
Converts a hexadecimal ASCII string to a byte array.
byte mask = (byte) 0x7F;
byte[] bytes = new byte[0];
if (hexStr != null) {
    hexStr = hexStr.toUpperCase();
    int len = hexStr.length();
    bytes = new byte[(len / 2)];
    int sPos = 0; 
    int bPos = 0; 
...
byte[]toBytes(String hexString)
to Bytes
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
    result[i] = Integer.valueOf(hexString.substring(2 * i, (2 * i) + 2), 16).byteValue();
return result;
byte[]toBytes(String hexString)
Get the byte representation of an ASCII-HEX string.
if (hexString == null || hexString.length() % 2 != 0) {
    throw new RuntimeException("Input string must contain an even number of characters");
char[] hex = hexString.toCharArray();
int length = hex.length / 2;
byte[] raw = new byte[length];
for (int i = 0; i < length; i++) {
    int high = Character.digit(hex[i * 2], 16);
...
byte[]toBytes(String name)
to Bytes
return name.getBytes();
longtoBytes(String s)
to Bytes
long size = 0;
if (s.endsWith("K") || s.endsWith("k")) {
    size = Long.valueOf(s.substring(0, s.length() - 1).trim()) * 1024;
} else if (s.endsWith("M") || s.endsWith("m")) {
    size = Long.valueOf(s.substring(0, s.length() - 1).trim()) * 1024 * 1024;
} else if (s.endsWith("G") || s.endsWith("g")) {
    size = Long.valueOf(s.substring(0, s.length() - 1).trim()) * 1024 * 1024;
return size;
byte[]toBytes(String s, int len, byte pad)
Routine to convert a string to bytes and pad with a character up to a given length.
if (s == null)
    throw new IllegalArgumentException("null string provided");
byte[] out = new byte[len];
for (int i = 0; i < len; i++) {
    if (i > s.length()) {
        out[i] = pad;
    } else {
        out[i] = (byte) s.charAt(i);
...
inttoBytes(String str, byte[] bytes, int index)
Convert string to bytes and store them in the array starting at the given index.
int typeBytes = Character.SIZE / Byte.SIZE;
checkBounds(bytes, index, typeBytes * str.length());
for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    for (int j = typeBytes - 1; j >= 0; j--) {
        int shiftBits = Byte.SIZE * j;
        byte b = (byte) (c >> shiftBits);
        bytes[index++] = b;
...