zoukankan      html  css  js  c++  java
  • unicode编码转汉字

    public static void main(String[] args) throws UnsupportedEncodingException  {
     int y = 20892;
    decodeUnicode("U+"+Integer.toHexString(y))
    }
     
    //中文转Unicode  
          public static String gbEncoding(final String gbString) {   //gbString = "测试" 
        char[] utfBytes = gbString.toCharArray();   //utfBytes = [测, 试]  
               String unicodeBytes = "";    
        for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {     

                String hexB = Integer.toHexString(utfBytes[byteIndex]);   //转换为16进制整型字符串 

        if (hexB.length() <= 2) {     

                    hexB = "00" + hexB;     

    1.                }     
    2.                unicodeBytes = unicodeBytes + "\u" + hexB;     
    3.           }     
    4.           System.out.println("unicodeBytes is: " + unicodeBytes);     
    5. return unicodeBytes;     
    6.       }  


     
      1. //Unicode转中文  
      2.       public static String decodeUnicode(final String dataStr) {     
      3.          int start = 0;     
      4.          int end = 0;     
      5.          final StringBuffer buffer = new StringBuffer();     
      6.          while (start > -1) {     
      7.              end = dataStr.indexOf("\u", start + 2);     
      8.              String charStr = "";     
      9.              if (end == -1) {     
      10.                  charStr = dataStr.substring(start + 2, dataStr.length());     
      11.              } else {     
      12.                  charStr = dataStr.substring(start + 2, end);     
      13.              }     
      14.              char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。     
      15.              buffer.append(new Character(letter).toString());     
      16.              start = end;     
      17.          }     
      18.          return buffer.toString();     
      19.       } 
  • 相关阅读:
    Beta阶段DAY1
    Beta阶段DAY2
    Beta阶段冲刺前准备
    Beta阶段Scrum 冲刺博客合集
    alpha阶段项目复审
    网络15软工个人作业5——软件工程总结
    软工网络15个人作业4——alpha阶段个人总结
    软件工程网络15个人作业3 (201521123051 谢庆圆)
    软件工程网络15专业结对编程
    软工网络15个人阅读作业2 (201521123051 谢庆圆)
  • 原文地址:https://www.cnblogs.com/kongxc/p/6344554.html
Copyright © 2011-2022 走看看