Android Hex String to Byte Array Convert hexStringToBytes(String hexString)

Here you can find the source of hexStringToBytes(String hexString)

Description

hex String To Bytes

License

Open Source License

Declaration

public static byte[] hexStringToBytes(String hexString) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }//from w  ww .ja  va 2  s.  com
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     * 
     * @param c
     *            char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}

Related

  1. fromHex(String hex)
  2. fromHex(String hexData)
  3. hexStringToByteArray(String s)
  4. hexStringToBytes(String hex)
  5. hexStringToBytes(String hex)
  6. hexStringToBytes(String hexString)
  7. toByte(String hexString)
  8. toByte(String hexString)
  9. hex2byte(String inputString)