Unicode编码与中文互转


 1 /**
 2      * unicode编码转换为汉字
 3      * @param unicodeStr 待转化的编码
 4      * @return 返回转化后的汉子
 5      */
 6     public static String UnicodeToCN(String unicodeStr) {
 7         Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
 8         Matcher matcher = pattern.matcher(unicodeStr);
 9         char ch;
10         while (matcher.find()) {
11             //group
12             String group = matcher.group(2);
13             //ch:'李四' 
14             ch = (char) Integer.parseInt(group, 16);
15             //group1 
16             String group1 = matcher.group(1);
17             unicodeStr = unicodeStr.replace(group1, ch + "");
18         }
19         
20         return unicodeStr.replace("\\", "").trim();
21     }
/**
     * 汉字转化为Unicode编码
     * @param CN 待转化的中文
     * @return 返回转化之后的unicode编码
     */
    public static String CNToUnicode(String CN) {
        
        try {
            StringBuffer out = new StringBuffer("");
            //直接获取字符串的unicode二进制
            byte[] bytes = CN.getBytes("unicode");
            //然后将其byte转换成对应的16进制表示即可
            for (int i = 0; i < bytes.length - 1; i += 2) {
                out.append("\\u");
                String str = Integer.toHexString(bytes[i + 1] & 0xff);
                for (int j = str.length(); j < 2; j++) {
                    out.append("0");
                }
                String str1 = Integer.toHexString(bytes[i] & 0xff);
                out.append(str1);
                out.append(str);
            }
            return out.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }

测试

1 public static void main(String[] args) {
2         String Unicodestr = "\\u674e\\u56db";
3         System.out.println("unicode为\\u674e\\u56db对应的中文是:"+Util.UnicodeToCN(Unicodestr));
4         String CNStr = "李四";
5         System.out.println("李四对应的Unicode编码是:"+Util.CNToUnicode(CNStr));
6         
7     }

测试结果:

这里可能需要解释的是:\ufeff。\ufeff表示的是UTF-16(大端序)的编码方式。在显示的时候可以将\ufeff过滤掉


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM