美文网首页
16进制unicode解码

16进制unicode解码

作者: chuan_bai | 来源:发表于2017-09-26 11:32 被阅读180次
    public static String toUnicode(String str) {
            str = (str == null ? "" : str);
            if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回
                return str;
            StringBuffer sb = new StringBuffer(1000);
            for (int i = 0; i < str.length() - 6; ) {
                String strTemp = str.substring(i, i + 6);
                String value = strTemp.substring(2);
                int c = 0;
                for (int j = 0; j < value.length(); j++) {
                    char tempChar = value.charAt(j);
                    int t = 0;
                    switch (tempChar) {
                        case 'a':
                            t = 10;
                            break;
                        case 'b':
                            t = 11;
                            break;
                        case 'c':
                            t = 12;
                            break;
                        case 'd':
                            t = 13;
                            break;
                        case 'e':
                            t = 14;
                            break;
                        case 'f':
                            t = 15;
                            break;
                        default:
                            t = tempChar - 48;
                            break;
                    }
                    c += t * ((int) Math.pow(16, (value.length() - j - 1)));
                }
                sb.append((char) c);
                i = i + 6;
            }
            return sb.toString();
        }
    

    相关文章

      网友评论

          本文标题:16进制unicode解码

          本文链接:https://www.haomeiwen.com/subject/kwksextx.html