zoukankan      html  css  js  c++  java
  • java unicode转中文

    public static String unicodeToString(String str) {
     
        Pattern pattern = Pattern.compile("(\\u(\p{XDigit}{4}))");    
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");    
        }
        return str;
    }

    获取字符串的unicode编码,这个我们可以通过直接获取字符串的unicode二进制,然后将其byte转换成对应的16进制表示

    static String getUnicode(String s) {
            try {
                StringBuffer out = new StringBuffer("");
                byte[] bytes = s.getBytes("unicode");
                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;
            }
        }

    http://www.cnblogs.com/xignzou/p/3329438.html

  • 相关阅读:
    ubuntu 16.04 安装显卡驱动,再安装cuda
    8. golang 基本类型转换
    7.golang的字符串 string
    5. 变量定义
    4. 代码规范
    3.golang 的注释
    1.windows server 201x
    exec 命令
    powershell
    1.Dockerfile
  • 原文地址:https://www.cnblogs.com/peterpanzsy/p/4271861.html
Copyright © 2011-2022 走看看