• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
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过滤掉

posted @ 2018-09-29 11:21  Java小斌  阅读(13580)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3