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     
posted @ 2012-07-23 08:54  将来的老大爷  阅读(970)  评论(0编辑  收藏  举报

如果本页面列出的内容侵犯了您的权益,请告知。
知识共享许可协议
996.icu