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

inttoBytes(Object value)
Converts a string to a bytes integer, interpreting 'b', 'kb', 'mb', 'gb' and 'tb' suffixes.
if (value == null)
    return 0;
if (value instanceof Number)
    return ((Number) value).intValue();
else {
    String s = value.toString();
    if (s.endsWith("kb"))
        return (int) (Float.parseFloat(s.substring(0, s.length() - 2)) * 1024f);
...
byte[]toBytes(short s)
to Bytes
byte[] bytes = new byte[2];
bytes[0] = (byte) (s & 0xff);
bytes[1] = (byte) ((s >> 8) & 0xff);
return bytes;
byte[]toBytes(short s)
to Bytes
return new byte[] { (byte) (s & 0x00FF), (byte) ((s & 0xFF00) >> 8) };
voidtoBytes(short sVal, byte[] bytes, boolean bigEndian)
Converts a short into a byte array.
if (bigEndian) {
    bytes[0] = (byte) (sVal >> 8);
    bytes[1] = (byte) (sVal & 0xff);
} else {
    bytes[0] = (byte) (sVal & 0xff);
    bytes[1] = (byte) (sVal >> 8);
byte[]toBytes(short value)
Convert a short to a byte array
byte[] bytes = new byte[2];
toBytes(value, bytes, 0);
return bytes;
byte[]toBytes(short value)
to Bytes
byte[] buf = new byte[2];
toBytes(value, buf);
return buf;
byte[]toBytes(String hex)
Convert hex string to byte array.
String data = hex.length() % 2 == 1 ? "0" + hex : hex;
byte[] result = new byte[data.length() / 2];
for (int i = 0; i < result.length; i++) {
    result[i] = (byte) Integer.parseInt(data.substring(2 * i, 2 * i + 2), 16);
return result;
byte[]toBytes(String hex)
to Bytes
byte[] bts = new byte[hex.length() / 2];
for (int i = 0; i < bts.length; i++) {
    bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
return bts;
byte[]toBytes(String hex)
Convert Hex string to byte array hex string format is: 2 char represent one byte,can use space as separator for example, "12 1e 3d ee FF 09"
byte[] buff = new byte[hex.length() / 2];
int s1, s2, count = 0;
for (int i = 0; i < hex.length(); i++) {
    if (hex.charAt(i) >= '0' && hex.charAt(i) <= '9')
        s1 = hex.charAt(i) - 48;
    else if (hex.charAt(i) >= 'A' && hex.charAt(i) <= 'F')
        s1 = hex.charAt(i) - 55;
    else if (hex.charAt(i) >= 'a' && hex.charAt(i) <= 'f')
...
byte[]toBytes(String hex)
Convert a hex string to a byte[].
if (hex == null || hex.length() % 2 != 0)
    throw new IllegalArgumentException();
byte[] ret = new byte[hex.length() / 2];
for (int i = 0, j = 0; i < hex.length(); i += 2, j++) {
    int high = charToNibble(hex.charAt(i));
    int low = charToNibble(hex.charAt(i + 1));
    ret[j] = (byte) ((high << 4) | low);
return ret;