Android Utililty Methods Hex String to Byte Array Convert

List of utility methods to do Hex String to Byte Array Convert

Description

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

Method

StringfromHex(String hex)
from Hex
return new String(toByte(hex));
byte[]fromHex(String hexData)
Converts a Hex-encoded data string to the original byte data.
byte[] result = new byte[(hexData.length() + 1) / 2];
String hexNumber = null;
int stringOffset = 0;
int byteOffset = 0;
while (stringOffset < hexData.length()) {
    hexNumber = hexData.substring(stringOffset, stringOffset + 2);
    stringOffset += 2;
    result[byteOffset++] = (byte) Integer.parseInt(hexNumber, 16);
...
byte[]hexStringToByteArray(String s)
hex String To Byte Array
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
    data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
            .digit(s.charAt(i + 1), 16));
return data;
byte[]hexStringToBytes(String hex)
hex String To Bytes
int len = (hex.length() / 2);
byte[] res = new byte[len];
char[] c = hex.toCharArray();
for (int i = 0; i < len; i++) {
    int pos = i * 2;
    res[i] = (byte) (toByte(c[pos]) << 4 | toByte(c[pos + 1]));
return res;
...
byte[]hexStringToBytes(String hex)
hex String To Bytes
if (0 != hex.length() % 2)
    throw new IllegalArgumentException(hex
            + " not be a valid HEX String!");
byte[] result = new byte[hex.length() >> 1];
int high_nibble;
int low_nibble;
for (int i = 0, j = 0; i < result.length; i++, j += 2) {
    high_nibble = Character.digit(hex.charAt(j), 16) << 4;
...
byte[]hexStringToBytes(String hexString)
hex String To Bytes
if (hexString == null || hexString.equals("")) {
    return null;
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
...
byte[]hexStringToBytes(String hexString)
hex String To Bytes
if (hexString == null || hexString.equals("")) {
    return null;
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
...
byte[]toByte(String hexString)
to Byte
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)).byteValue();
return result;
byte[]toByte(String hexString)
Retrieve the bytes data
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[]hex2byte(String inputString)
hexbyte
if (inputString == null || inputString.length() < 2) {
    return new byte[0];
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
    String tmp = inputString.substring(2 * i, 2 * i + 2);
...