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.       } 
  • 相关阅读:
    计算机视觉资源合集
    EMZ-搭建DL模型的最简便的方式 | 附项目地址
    在边缘设备上拟合大型神经网络的方法总结
    归一化方法总结 | 又名“BN和它的后浪们“
    名词解释 | 论文中的Ablation study
    知识蒸馏的简要概述
    如何看待计算机视觉未来的走向(二)从产品的角度聊一聊
    STL源码分析--functional
    STL源码分析--algorithm
    STL源码分析--bitset
  • 原文地址:https://www.cnblogs.com/kongxc/p/6344554.html
Copyright © 2011-2022 走看看