zoukankan      html  css  js  c++  java
  • 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过滤掉

  • 相关阅读:
    mysql那些事(1)手机号与座机号码如何存储
    分享一个PHP调用RestFul接口的函数
    php sprintf用法
    HTTP状态码详解
    PHP随机生成中国人姓名的类
    PHP计算两组经纬度坐标之间的距离
    PHP根据经纬度获取在范围坐标的数据
    PHP 利用QQ邮箱发送邮件「PHPMailer」
    PHP中利用PHPMailer配合QQ邮箱实现发邮件
    修改PHP上传文件大小限制
  • 原文地址:https://www.cnblogs.com/lihuibin/p/9722742.html
Copyright © 2011-2022 走看看