Java學習

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

           

          中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現

          平時開發中,經常遇到中文轉換成中文轉換成Unicode編碼和Unicode編碼轉換成中文的問題,國際化的時候,也要遇到這個問題,
          現在我就把中網上找的很自己學習的經驗,共享給大家了。閑話少說,步入正題,Java代碼如下:
           轉貼請著名:http://www.aygfsteel.com/jerry-zhaoj/

          1. package test.com.gjob.services;
          2. import java.util.Properties;
          3.  public class Test { 
          4.         public static void main(String[] args) { 
          5.         String s = "簡介"; 
          6.        String tt = gbEncoding(s); 
          7. //       String tt1 = "你好,我想给你说一个事情";
          8.        System.out.println(decodeUnicode("\\u7b80\\u4ecb")); 
          9. //       System.out.println(decodeUnicode(tt1)); 
          10.        System.out.println(HTMLDecoder.decode("中国"));
          11.        String s1 = "\u7b80\u4ecb";
          12.        System.out.println(s.indexOf("\\"));
          13.       } 
          14.      public static String gbEncoding(final String gbString) { 
          15.      char[] utfBytes = gbString.toCharArray(); 
          16.            String unicodeBytes = ""; 
          17.             for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) { 
          18.                  String hexB = Integer.toHexString(utfBytes[byteIndex]); 
          19.                    if (hexB.length() <= 2) { 
          20.                        hexB = "00" + hexB; 
          21.                   } 
          22.                    unicodeBytes = unicodeBytes + "\\u" + hexB; 
          23.                } 
          24.                System.out.println("unicodeBytes is: " + unicodeBytes); 
          25.                return unicodeBytes; 
          26.           } 
          27.        
          28.          public static String decodeUnicode(final String dataStr) { 
          29.             int start = 0; 
          30.               int end = 0; 
          31.              final StringBuffer buffer = new StringBuffer(); 
          32.               while (start > -1) { 
          33.                  end = dataStr.indexOf("\\u", start + 2); 
          34.                   String charStr = ""; 
          35.                   if (end == -1) { 
          36.                       charStr = dataStr.substring(start + 2, dataStr.length()); 
          37.                  } else { 
          38.                      charStr = dataStr.substring(start + 2, end); 
          39.                   } 
          40.                   char letter = (char) Integer.parseInt(charStr, 16); // 16進制parse整形字符串。 
          41.                 buffer.append(new Character(letter).toString()); 
          42.                 start = end; 
          43.               } 
          44.               return buffer.toString(); 
          45.           } 
          46.       } 

          posted on 2009-03-24 16:09 找個美女做老婆 閱讀(7383) 評論(9)  編輯  收藏

          評論

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2009-10-12 09:56 lzx

          非常好,正好可以解決我的問題  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2009-10-14 16:01 找個美女做老婆

          有現成的工具,可以到Java樂園 http://www.javaly.cn/javahome/convertunicode.faces 轉換  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2010-01-08 20:17 someone

          文章排版太差了。。。  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2012-01-18 16:08 nimeide

          SB的排版。。。  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 [未登錄] 2012-03-30 15:37 123123

          真他媽傻逼  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2012-07-08 19:46 找小姐上門服務信息

          找小姐上門服務信息  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2012-12-27 13:28 啊啊啊啊啊

          蛋疼  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 2012-12-27 13:29 找小姐上門服務信息

          無聊……  回復  更多評論   

          # re: 中文轉換成Unicode編碼和Unicode編碼轉換成中文,Java代碼實現 [未登錄] 2014-05-07 14:44 00

          贊一個  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           

          導航

          統計

          公告

          本blog已經搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平舆县| 江孜县| 焦作市| 青河县| 聂拉木县| 清水河县| 神木县| 南陵县| 三原县| 兰西县| 宁海县| 武汉市| 仁怀市| 峨眉山市| 马关县| 金阳县| 金川县| 盐津县| 全州县| 遵义市| 禹州市| 勃利县| 贵德县| 濮阳市| 昌平区| 张掖市| 遂宁市| 诏安县| 高尔夫| 松桃| 历史| 绥德县| 昌吉市| 福清市| 望都县| 靖边县| 大连市| 大安市| 浏阳市| 大港区| 定南县|