• 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.       } 
  • 相关阅读:
    多线程(一)
    Interface
    Abstract
    面向对象
    字符串比对(10分)
    快递费用计算(7分)
    练习7-8 方阵循环右移 (20分)
    习题4-11 兔子繁衍问题 (15分)
    一道题理解穷举/贪心/爬山/遗传/退火/蚁群算法
    华为暑期测试实习生面经(2020.06)
  • 原文地址:https://www.cnblogs.com/kongxc/p/6344554.html
Copyright © 2020-2023  润新知