Java Utililty Methods Hex to Byte Array

List of utility methods to do Hex to Byte Array

Description

The list of methods to do Hex to Byte Array are organized into topic(s).

Method

byte[]convertHexAsciiToByteArray(byte[] bytes)
convert Hex Ascii To Byte Array
return convertHexAsciiToByteArray(bytes, 0, bytes.length);
byte[]convertHEXString2ByteArray(String value)
convert HEX String Byte Array
if (value == null || value.length() == 0) {
    return null;
} else {
    char[] array = value.toCharArray();
    int ext = array.length % 2; 
    byte[] out = new byte[array.length / 2 + ext];
    for (int i = 0; i < array.length - ext; i += 2) {
        String part = new String(array, i, 2);
...
byte[]convertHexStringToByteArray(String hexString)
convert Hex String To Byte Array
if ((hexString.length() % 2) != 0) {
    throw new IllegalArgumentException("Invalid hex string (length % 2 != 0)");
byte[] array = new byte[hexString.length() / 2];
for (int i = 0, arrayIndex = 0; i < hexString.length(); i += 2, arrayIndex++) {
    array[arrayIndex] = Integer.valueOf(hexString.substring(i, i + 2), 16).byteValue();
return array;
...
byte[]convertHexStringToByteArray(String s)
convert Hex String To Byte Array
s = s.replace(" 0x", "");
s = s.replace(":0x", "");
s = s.replace(" ", "");
s = s.replace(":", "");
final int len = s.length();
final int rem = len % 2;
byte[] ret = new byte[len / 2 + rem];
if (rem != 0) {
...
byte[]convertHexStringToByteArray(String str, int numBytes, int numCharsPerByte)
Convert raw memory data to byte array
if (str.length() == 0)
    return null;
StringBuffer buf = new StringBuffer(str);
int requiredPadding = numBytes * numCharsPerByte - str.length();
while (requiredPadding > 0) {
    buf.insert(0, "0"); 
    requiredPadding--;
byte[] bytes = new byte[numBytes];
str = buf.toString();
for (int i = 0; i < bytes.length; i++) {
    String oneByte = str.substring(i * 2, i * 2 + 2);
    Integer number = Integer.valueOf(oneByte, 16);
    if (number.compareTo(Integer.valueOf(Byte.toString(Byte.MAX_VALUE))) > 0) {
        int temp = number.intValue();
        temp = temp - 256;
        String tempStr = Integer.toString(temp);
        Byte myByte = Byte.valueOf(tempStr);
        bytes[i] = myByte.byteValue();
    } else {
        Byte myByte = Byte.valueOf(oneByte, 16);
        bytes[i] = myByte.byteValue();
return bytes;
byte[]convertHexStringToByteNoSpace(String s)
convert Hex String To Byte No Space
int len = s.length();
byte[] ret = new byte[len >>> 1];
for (int i = 0; i <= len - 2; i += 2) {
    ret[i >>> 1] = (byte) (Integer.parseInt(s.substring(i, i + 2).trim(), 16) & 0xFF);
return ret;
byte[]convertHexStringToBytes(String hex)
Converts a hexadecimal String (such as one generated by the #convertBytesToHexString(byte[]) method) into an array of bytes.
if (hex.length() % 2 != 0) {
    throw new IllegalArgumentException("Hex string must have even number of characters.");
byte[] seed = new byte[hex.length() / 2];
for (int i = 0; i < seed.length; i++) {
    int index = i * 2;
    seed[i] = (byte) Integer.parseInt(hex.substring(index, index + 2), 16);
return seed;
byte[]convertHexStringToBytes(String hex)
Converts the specified hex string to a byte array.
int len = hex.length() / 2;
byte[] buf = new byte[len];
for (int i = 0; i < len; i++) {
    buf[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
return buf;
byte[]convertHexToByteArray(String hexString)
convert Hex To Byte Array
if ((hexString.length() % 2) != 0) {
    throw new IllegalArgumentException();
byte[] result = new byte[hexString.length() / 2];
char[] enc = hexString.toCharArray();
for (int i = 0; i < enc.length; i += 2) {
    StringBuilder curr = new StringBuilder(2);
    curr.append(enc[i]).append(enc[i + 1]);
...
byte[]convertHexToBytes(String s)
Convert a hex encoded string to a byte array.
int len = s.length();
if (len % 2 != 0) {
    throw new RuntimeException("HEX_STRING_ODD:" + s);
len /= 2;
byte[] buff = new byte[len];
int mask = 0;
int[] hex = HEX_DECODE;
...