decode UTF8 to char : UTF « Development « Android






decode UTF8 to char

 

class Main {
    public static final String HEX_DIGITS = "0123456789ABCDEF";

    public static char decodeUTF8(String src) {
        if (src == null) {
            throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
        }

        if (!(src.startsWith("\\u") && src.length() <= 6)) {
            throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
        }

        char[] sources = src.substring(2).toCharArray();
        char res = 0;
        for (char nextChar : sources) {
            int digit = HEX_DIGITS.indexOf(Character.toUpperCase(nextChar));
            res = (char) (res * 16 + digit);
        }
        return res;
    }

}

   
  








Related examples in the same category

1.decode UTF8 to String
2.implements InputFilter
3.UTF 2 GBUtil
4.Load UTF8 encoded file to String
5.UTF8 to Byte Array