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.       } 
  • 相关阅读:
    iOS中NSString常用操作合集
    iOS利用Runtime自定义控制器POP手势动画(经典)
    iOS学习之UILable上显示不同的字体和颜色
    iOS学习之UICollectionView使用注解
    iOS学习之new与alloc init,[NSArray array] 和 [[NSArray alloc]init] 及 self. 和 _ 的区别
    iOS学习之常用第三方框架总结(经典/必看)
    iOS学习之block总结及block内存管理(必看)
    Http协议与TCP协议理解
    SDImage框架实现原理详解
    iOS NSFileHandle常用操作
  • 原文地址:https://www.cnblogs.com/kongxc/p/6344554.html
Copyright © 2011-2022 走看看