java Unicode转码

 1     //中文转UNICODE
 2     public static String chinaToUnicode(String str) {
 3         String result = "";
 4         for (int i = 0; i < str.length(); i++) {
 5             int chr1 = (char) str.charAt(i);
 6             if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
 7                 result += "\\u" + Integer.toHexString(chr1);
 8             } else {
 9                 result += str.charAt(i);
10             }
11         }
12         return result;
13     }
1     //UNICODE转中文
2     public static String unicodeToChina(String str) throws Exception{
3         byte[] byteArr = str.getBytes("UTF-8");
4         String chinese=new String(byteArr,"UTF-8");
5         return chinese;
6     }
7     
Author:Pale Life
From: 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/live365wang/p/2604404.html