public static byte[] fromHexString(final String hexString) {
    if (hexString == null) {
      return null;
    }

    String str = hexString.trim().toLowerCase();

    // Strip any leading "0x"
    if (str.startsWith("0x")) {
      str = str.substring(2);
    }

    int numChars = str.length();
    if (numChars == 0) {
      return null;
    }
    char[] chars = str.toCharArray();
    byte[] retVal = new byte[(numChars + 1) / 2];
    int byteIndex = 0;
    int i = 0;
    if ((chars.length & 0x01) == 1) {
      retVal[byteIndex++] = fromChar(chars[i++]);
    }
    for (; i <chars.length; i += 2, ++byteIndex) {
      retVal[byteIndex] = (byte) ((fromChar(chars[i]) << 4) | fromChar(chars[i+1]));
    }
    return retVal;
  }