code: public static byte[] hexToBytes(String s) {
          try {
              if (s == null) return new byte[0];
              s = s.toUpperCase();
              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;
          } catch (StringIndexOutOfBoundsException e) {
              return null;
          }
      }
docs: Convert a hexadecimal string to a byte array.
  @param s The hexadecimal string to convert.
  @return The byte array of that string. Null if conversion was not possible.