public static byte[] toBytesBinary(String in) {
    // this may be bigger than we need, but let's be safe.
    byte[] b = new byte[in.length()];
    int size = 0;
    for (int i = 0; i < in.length(); ++i) {
      char ch = in.charAt(i);
      if (ch == '\\' && in.length() > i + 1 && in.charAt(i + 1) == 'x') {
        // ok, take next 2 hex digits.
        char hd1 = in.charAt(i + 2);
        char hd2 = in.charAt(i + 3);

        // they need to be A-F0-9:
        if (!isHexDigit(hd1) || !isHexDigit(hd2)) {
          // bogus escape code, ignore:
          continue;
        }
        // turn hex ASCII digit -> number
        byte d = (byte) ((toBinaryFromHex((byte) hd1) << 4) + toBinaryFromHex((byte) hd2));

        b[size++] = d;
        i += 3; // skip 3
      } else {
        b[size++] = (byte) ch;
      }
    }
    // resize:
    byte[] b2 = new byte[size];
    System.arraycopy(b, 0, b2, 0, size);
    return b2;
  }
Takes a ASCII digit in the range A-F0-9 and returns the corresponding integer/ordinal value.
@param ch The hex digit.
@return The converted hex value as a byte.
hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java