posts - 262,  comments - 221,  trackbacks - 0
          package example.encoding;

          /**
           * The Class ChineseValidator.
           
          */

          public class CharacterValueConveter {

              
          /**
               * The main method.
               * 
               * 
          @param args the arguments
               
          */

              
          public static void main(String args[]) {
                  CharacterValueConveter conveter 
          = new CharacterValueConveter();
                  conveter.testConvertion(
          "中文");
              }


              
          /**
               * Test convertion.
               * 
               * 
          @param content the content
               
          */

              
          public void testConvertion(String content) {
                  
          byte[] bytes = content.getBytes();
                  
                  System.out.println(
          "Original String : " + content);
                  System.out.println();
                  
                  
          // Get bytes array
                  System.out.println("Convert string to bytes array : ");
                  
          for (int i = 0; i < bytes.length; i++{
                      System.out.println(
          " byte[" + i + "]=" + bytes[i]);
                  }

                  System.out.println();

                  
          // Get each character and unicode
                  System.out.println("Start to convert by character: " + "\n");
                  
          for (int j = 0; j < content.length(); j++{
                      
          char c = content.charAt(j);
                      printAllValue(c);
                  }

              }


              
          /**
               * Prints the all value.
               * 
               * 
          @param c the c
               
          */

              
          public void printAllValue(char c) {

                  
          byte b = (byte) c;
                  
          int i = (int) c;
                  
          short s = (short) c;
                  String binaryString 
          = Integer.toBinaryString(i);
                  String hexString 
          = Integer.toHexString(i);

                  StringBuffer output 
          = new StringBuffer();
                  
          // Base value
                  output.append("Character : ").append(c).append("\n");
                  output.append(
          " byte value : ").append(b).append("\n");
                  output.append(
          " int value : ").append(i).append("\n");
                  output.append(
          " short value : ").append(s).append("\n");
                  
                  
          // Hex value and binary value
                  output.append(" hex value :").append(hexString).append("\n");
                  output.append(
          " binary value : ").append(binaryString).append("\n");
                  
                  
          // Unicode value in java file & web page with decimal encoding
                  output.append(" unicode value (Base 10): ");
                  output.append(
          "\\u").append(i).append(";").append("\n");
                  
                  output.append(
          " unicode value (Base 10 in web page): ");
                  output.append(
          "&#").append(i).append(";").append("\n");
                  
                  
          // Unicode value in java file & web page with hexadecimal encoding
                  output.append(" unicode value (Base 16): ");
                  output.append(
          "\\u").append(hexString).append(";").append("\n");
                  
                  output.append(
          " unicode value (Base 16 in web page): ");
                  output.append(
          "&#").append(hexString).append(";").append("\n");

                  System.out.println(output.toString());
              }


          }

          最終的測試結果如下:
          Original String : 中文

          Convert string to bytes array : 
           byte
          [0]=-42
           byte
          [1]=-48
           byte
          [2]=-50
           byte
          [3]=-60

          Start to convert by character: 

          Character : 中
           byte value : 
          45
           int value : 
          20013
           short value : 
          20013
           hex value :4e2d
           binary value : 
          100111000101101
           unicode value (Base 
          10): \u20013;
           unicode value (Base 10 in web page): &#20013;
           unicode value (Base 16): \u4e2d;
           unicode value (Base 16 in web page): e2d;

          Character : 文
           byte value : -
          121
           int value : 
          25991
           short value : 
          25991
           hex value :
          6587
           binary value : 
          110010110000111
           unicode value (Base 
          10): \u25991;
           unicode value (Base 10 in web page): &#25991;
           unicode value (Base 16): \u6587;
           unicode value (Base 16 in web page): &#6587;

          可以看到在Java中,字符的unicode有兩種表示顯示:一種是10進制形式,一種是16進制形式。它們可以分別通過:int i = (int)(string.charAt(i))和Integer.toHexString(i);獲得。而且在java文件和Web頁面,同一個unicode的表示形式是不同。web頁面需要用&#進行轉義,在java文件中則使用\u進行轉義。


          -------------------------------------------------------------
          生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
          posted on 2010-02-23 16:48 Paul Lin 閱讀(2131) 評論(2)  編輯  收藏 所屬分類: J2SE


          FeedBack:
          # re: 【Java基礎專題】編碼與亂碼(06)---字符的各種值轉換
          2010-02-24 05:23 | tbw
          學習了。。。。  回復  更多評論
            
          # re: 【Java基礎專題】編碼與亂碼(06)---字符的各種值轉換
          2010-02-24 14:22 | 咖啡妝
          期望更深入的講解  回復  更多評論
            
          <2010年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28123456
          78910111213

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          BlogJava熱點博客

          好友博客

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 石林| 汶上县| 浦江县| 天长市| 察隅县| 荆门市| 原平市| 建阳市| 博乐市| 堆龙德庆县| 海阳市| 扶绥县| 昂仁县| 南江县| 阿坝县| 梁河县| 龙山县| 南安市| 勃利县| 静安区| 杂多县| 三台县| 孟州市| 永安市| 铜川市| 吉安县| 凌海市| 三亚市| 宁武县| 从化市| 富阳市| 昂仁县| 乌兰浩特市| 永年县| 万载县| 钟祥市| 岑溪市| 大邑县| 卓尼县| 汽车| 白沙|