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

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 ww  w  .j  a v  a 2  s  .  c  o  m*/
        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;
    }

    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}

Related

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