<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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());
        }


    }

    最終的測(cè)試結(jié)果如下:
    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進(jìn)制形式,一種是16進(jìn)制形式。它們可以分別通過:int i = (int)(string.charAt(i))和Integer.toHexString(i);獲得。而且在java文件和Web頁(yè)面,同一個(gè)unicode的表示形式是不同。web頁(yè)面需要用&#進(jìn)行轉(zhuǎn)義,在java文件中則使用\u進(jìn)行轉(zhuǎn)義。


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


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

    常用鏈接

    留言簿(21)

    隨筆分類

    隨筆檔案

    BlogJava熱點(diǎn)博客

    好友博客

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲一区二区三区丝袜| 亚洲一区二区三区乱码A| 3d动漫精品啪啪一区二区免费| 国产精品成人免费观看| 男女作爱免费网站| 老司机午夜在线视频免费观| 色偷偷亚洲男人天堂| 在线观看亚洲专区| 免费无码午夜福利片69| 免费大片av手机看片| 一级免费黄色毛片| 精精国产www视频在线观看免费| 久久久久久国产a免费观看不卡| 久久性生大片免费观看性| 免费播放在线日本感人片| 久久成人免费大片| 一级毛片免费观看不卡视频| 久久国产色AV免费观看| 日本免费网址大全在线观看| 毛片免费观看的视频在线| 日韩免费高清一级毛片在线| 国产在线观看免费不卡 | 亚洲日本VA午夜在线电影| 亚洲精品成a人在线观看夫| 黄网站色视频免费看无下截| aa毛片免费全部播放完整| 免费视频一区二区| 天天影视色香欲综合免费| 成人免费视频软件网站| 青青青青青青久久久免费观看| 免费在线黄色网址| 亚洲av永久无码精品古装片| 亚洲毛片免费视频| 亚洲第一se情网站| 成人一区二区免费视频| 中文字幕视频免费| 在线免费观看韩国a视频| 国产性爱在线观看亚洲黄色一级片| 亚洲AV成人精品网站在线播放 | 亚洲AV之男人的天堂| 13小箩利洗澡无码视频网站免费|