Java學(xué)習(xí)

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

           

          中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn)

          平時開發(fā)中,經(jīng)常遇到中文轉(zhuǎn)換成中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文的問題,國際化的時候,也要遇到這個問題,
          現(xiàn)在我就把中網(wǎng)上找的很自己學(xué)習(xí)的經(jīng)驗(yàn),共享給大家了。閑話少說,步入正題,Java代碼如下:
           轉(zhuǎn)貼請著名: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進(jìn)制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: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2009-10-12 09:56 lzx

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

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2009-10-14 16:01 找個美女做老婆

          有現(xiàn)成的工具,可以到Java樂園 http://www.javaly.cn/javahome/convertunicode.faces 轉(zhuǎn)換  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2010-01-08 20:17 someone

          文章排版太差了。。。  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2012-01-18 16:08 nimeide

          SB的排版。。。  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) [未登錄] 2012-03-30 15:37 123123

          真他媽傻逼  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2012-07-08 19:46 找小姐上門服務(wù)信息

          找小姐上門服務(wù)信息  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2012-12-27 13:28 啊啊啊啊啊

          蛋疼  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) 2012-12-27 13:29 找小姐上門服務(wù)信息

          無聊……  回復(fù)  更多評論   

          # re: 中文轉(zhuǎn)換成Unicode編碼和Unicode編碼轉(zhuǎn)換成中文,Java代碼實(shí)現(xiàn) [未登錄] 2014-05-07 14:44 00

          贊一個  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 长沙市| 沧州市| 襄樊市| 宁波市| 江都市| 藁城市| 施甸县| 平遥县| 崇州市| 中超| 中牟县| 南木林县| 海城市| 安徽省| 吉水县| 桃江县| 从化市| 江达县| 财经| 西城区| 定日县| 哈密市| 江北区| 三明市| 奈曼旗| 温泉县| 镇巴县| 浮梁县| 阿荣旗| 当阳市| 松原市| 鹤庆县| 柳林县| 荥经县| 左权县| 金湖县| 修文县| 边坝县| 科尔| 太谷县| 家居|