java 中文與unicode互轉

來源:互聯網
上載者:User

標籤:[]   format   block   length   app   general   ++   字元   cep   

 

 

public class FontUtil {    public static void main(String[] args) {        System.out.println(chinaToUnicode("未登陸!"));        System.out.println(decodeUnicode("\u672a\u767b\u9646\uff01"));    }         /**      * 把中文轉成Unicode碼      *       * @param str      * @return      */      public static String chinaToUnicode(String str) {          String result = "";          for (int i = 0; i < str.length(); i++) {              int chr1 = (char) str.charAt(i);              if (chr1 >= 19968 && chr1 <= 171941) {// 漢字範圍 \u4e00-\u9fa5 (中文)                  result += "\\u" + Integer.toHexString(chr1);              } else {                  result += str.charAt(i);              }          }          return result;      }        /**      * 判斷是否為中文字元      *       * @param c      * @return      */      public static boolean isChinese(char c) {          Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);          if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS                  || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS                  || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A                  || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION                  || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION                  || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {              return true;          }          return false;      }              //Unicode轉中文      public static String decodeUnicode(final String unicode) {              StringBuffer string = new StringBuffer();                    String[] hex = unicode.split("\\\\u");              for (int i = 0; i < hex.length; i++) {                              try {                   // 漢字範圍 \u4e00-\u9fa5 (中文)                   if(hex[i].length()>=4){//取前四個,判斷是否是漢字                       String chinese = hex[i].substring(0, 4);                       try {                           int chr = Integer.parseInt(chinese, 16);                           boolean isChinese = isChinese((char) chr);                           //轉化成功,判斷是否在  漢字範圍內                           if (isChinese){//在漢字範圍內                               // 追加成string                               string.append((char) chr);                               //並且追加  後面的字元                               String behindString = hex[i].substring(4);                               string.append(behindString);                           }else {                               string.append(hex[i]);                           }                       } catch (NumberFormatException e1) {                           string.append(hex[i]);                       }                                          }else{                       string.append(hex[i]);                   }               } catch (NumberFormatException e) {                   string.append(hex[i]);               }           }              return string.toString();      }      }

 

java 中文與unicode互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.