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

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

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

    posts - 325,  comments - 25,  trackbacks - 0

    本例舉例三種常用的字符編碼”GBK"  "UTF-8"  "UTF-16LE"
    我們發(fā)現(xiàn),在使用String.getBytes這個方法時,
    若字符編碼參數(shù)使用”GBK“
    則遇字母占一個字節(jié),遇中文占兩個字節(jié)
    若字符編碼參數(shù)使用”UTF-8“
    則遇字母占一個字節(jié),遇中文占三個字節(jié)
    若字符編碼參數(shù)使用”UTF-16LE“
    則無論字母或中文均占兩個字節(jié)

    反之使用new String根據(jù)byte數(shù)組構(gòu)造字符串對象時,需要根據(jù)byte[]數(shù)組所使用的字符編碼進行相應轉(zhuǎn)化
    否則容易出現(xiàn)亂碼,


    package com.genius.demo;

    import java.io.UnsupportedEncodingException;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;

    public class StringConvertDemoActivity extends Activity {
        /** Called when the activity is first created. */
     
     
     
     private TextView textview1;
     private TextView textview2;
     private TextView textview3;
     private TextView textview4;
     
     
     

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
            init();
           
            print();
        }
       
        void init()
        {
         textview1 = (TextView) findViewById(R.id.print1);
         textview2 = (TextView) findViewById(R.id.print2);
         textview3 = (TextView) findViewById(R.id.print3);
         textview4 = (TextView) findViewById(R.id.print4);
        }
       
        void print()
        {
         String str1 = "eoe"; 
         byte[]s1d1 = null;
         byte[]s1d2 = null;
         byte[]s1d3 = null;
         try {
       s1d1 = str1.getBytes("GBK");
       s1d2 = str1.getBytes("UTF-8");
          s1d3 = str1.getBytes("UTF-16LE");  
          
          String print1 = str1 + " getbytes(gbk) size = " + s1d1.length + "\n" +
       str1 + " getbytes(UTF-8) size = " + s1d2.length + "\n" +
       str1 + " getbytes(UTF-16LE) size = " + s1d3.length + "\n";  

          textview1.setText(print1);
          
      } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
         
      
      
      
      
      
      String str2 = "社區(qū)";
      byte[]s2d1 = null;
         byte[]s2d2 = null;
         byte[]s2d3 = null;
         try {
          s2d1 = str2.getBytes("GBK");
          s2d2 = str2.getBytes("UTF-8");
          s2d3 = str2.getBytes("UTF-16LE");
          
          String print2 = str2 + " getbytes(gbk) size = " + s2d1.length + "\n" +
       str2 + " getbytes(UTF-8) size = " + s2d2.length + "\n" +
       str2 + " getbytes(UTF-16LE) size = " + s2d3.length + "\n";

          textview2.setText(print2);
      } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
          
      
      
      
      
      
      
      
      String str3 = "eoe社區(qū)";
      byte[]s3d1 = null;
         byte[]s3d2 = null;
         byte[]s3d3 = null;
         try {
          s3d1 = str3.getBytes("GBK");
          s3d2 = str3.getBytes("UTF-8");
          s3d3 = str3.getBytes("UTF-16LE");
          
          String print3 = str3 + " getbytes(gbk) size = " + s3d1.length + "\n" +
       str3 + " getbytes(UTF-8) size = " + s3d2.length + "\n" +
       str3 + " getbytes(UTF-16LE) size = " + s3d3.length + "\n";

          textview3.setText(print3); 
      } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return ;
      }
     
       
      
      
      
      
      
      try {
       String new1 = new String(str3.getBytes("GBK"), "GBK");
       String new2 = new String(str3.getBytes("GBK"), "UTF-8");
       String new3 = new String(str3.getBytes("GBK"), "UTF-16LE");
       
       String print4 = "new string(" + str3 + ".getbytes('gbk')), 'gbk') = " + new1 + "\n" +
           "new string(" + str3 + ".getbytes('gbk')), 'utf-8') = " + new2 + "\n" +
           "new string(" + str3 + ".getbytes('gbk')), 'utf-16le') = " + new3 + "\n";
       
       textview4.setText(print4); 
      } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return ;
      }
      
      
      
      
      
      
        }
       
       
       
       
       
       
    }

    posted on 2011-10-12 14:08 長春語林科技 閱讀(3468) 評論(0)  編輯  收藏 所屬分類: android
    <2011年10月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

     

    長春語林科技歡迎您!

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费va在线观看| 真实国产乱子伦精品免费| 成年女人18级毛片毛片免费观看| 亚洲AV电影院在线观看| a毛片全部免费播放| 国产亚洲精品资源在线26u| 人妻免费久久久久久久了| 亚洲AV无码乱码在线观看牲色 | 亚洲人成电影福利在线播放| 97人妻精品全国免费视频| 国产V亚洲V天堂无码久久久| 暖暖免费日本在线中文| 亚洲综合久久1区2区3区| 亚洲精品在线免费观看| 国产99在线|亚洲| 日本不卡高清中文字幕免费| 无人视频在线观看免费播放影院| 中文字幕无码精品亚洲资源网| sihu国产精品永久免费| 亚洲AV日韩精品久久久久久久 | 午夜毛片不卡高清免费| 无套内谢孕妇毛片免费看看| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 国产成人免费午夜在线观看| 亚洲一区二区三区无码国产| 一个人免费观看在线视频www| 亚洲精品GV天堂无码男同| 国产高清在线精品免费软件| a级毛片免费观看在线| 亚洲欧洲日产国码久在线观看| 成年女人色毛片免费看| 四虎一区二区成人免费影院网址| 亚洲AV本道一区二区三区四区| 中国在线观看免费国语版| 美女免费视频一区二区| 亚洲AV无码精品色午夜果冻不卡 | 免费人成大片在线观看播放电影| 亚洲无人区一区二区三区| 亚洲精品国产免费| 日韩毛片免费一二三| 91嫩草私人成人亚洲影院|