Android Utililty Methods String to Hex Byte Array Convert

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

Description

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

Method

byte[]stringToHexByte(String str)
string To Hex Byte
String hexString = bytesToHexString(str.getBytes());
return hexStringToBytes(hexString);
byte[]hexStringToBytes(String hexString)
Convert hex string to byte[]
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[]hexStr2Bytes(String src)
hex Str Bytes
int m = 0, n = 0;
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
    m = i * 2 + 1;
    n = m + 1;
    ret[i] = Byte.decode("0x" + src.substring(i * 2, m)
            + src.substring(m, n));
...
byte[]hexStringToBytes(String hex)
hex String To Bytes
int len = hex.length() / 2;
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
    int pos = i * 2;
    byte b1 = (byte) "0123456789ABCDEF".indexOf(achar[pos]);
    byte b2 = (byte) "0123456789ABCDEF".indexOf(achar[(pos + 1)]);
    result[i] = (byte) (b1 << 4 | b2);
...
byte[]getBytes(String hexString)
get Bytes
String[] hexArray = hexString.split(HEX_STRING_BLANK_SPLIT);
byte[] bytes = new byte[hexArray.length];
for (int i = 0; i < hexArray.length; i++) {
    String hex = hexArray[i];
    bytes[i] = Integer.valueOf(hex, 16).byteValue();
return bytes;
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[]stringToHex(String ids)
string To Hex
byte[] buf = new byte[ids.length() / 2];
for (int i = 0, j = 0; i < buf.length; i++, j += 2) {
    byte h = (byte) ((byte) ids.charAt(j) - 0x30);
    byte l = (byte) ((byte) ids.charAt(j + 1) - 0x30);
    buf[i] = (byte) (h << 4 | l);
return buf;
byte[]hexStringToBytes(String s)
hex String To Bytes
if (null == s)
    return null;
return hexStringToBytes(s, 0, s.length());
byte[]hexStringToBytes(String hexString, int offset, int count)
hex String To Bytes
if (null == hexString || offset < 0 || count < 2
        || (offset + count) > hexString.length())
    return null;
byte[] buffer = new byte[count >> 1];
int stringLength = offset + count;
int byteIndex = 0;
for (int i = offset; i < stringLength; i++) {
    char ch = hexString.charAt(i);
...
byte[]fromHex(String dataString)
Decode a hexadecimal string to a bytearray.
if (dataString.equals("")) {
    return null;
BigInteger dataPlus1BigInteger = new BigInteger("10" + dataString,
        16);
byte[] dataPlus1Bytes = dataPlus1BigInteger.toByteArray();
byte[] dataBytes = new byte[dataPlus1Bytes.length - 1];
System.arraycopy(dataPlus1Bytes, 1, dataBytes, 0, dataBytes.length);
...