<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"
    我們發現,在使用String.getBytes這個方法時,
    若字符編碼參數使用”GBK“
    則遇字母占一個字節,遇中文占兩個字節
    若字符編碼參數使用”UTF-8“
    則遇字母占一個字節,遇中文占三個字節
    若字符編碼參數使用”UTF-16LE“
    則無論字母或中文均占兩個字節

    反之使用new String根據byte數組構造字符串對象時,需要根據byte[]數組所使用的字符編碼進行相應轉化
    否則容易出現亂碼,


    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 = "社區";
      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社區";
      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 長春語林科技 閱讀(3458) 評論(0)  編輯  收藏 所屬分類: android
    <2011年10月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

     

    長春語林科技歡迎您!

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 中文无码日韩欧免费视频| 蜜臀亚洲AV无码精品国产午夜.| 本免费AV无码专区一区| 又色又污又黄无遮挡的免费视| 蜜桃传媒一区二区亚洲AV | 国产啪精品视频网免费| 亚洲成人午夜电影| 国产免费AV片在线播放唯爱网| 亚洲人成在线中文字幕| 18禁成年无码免费网站无遮挡 | 亚洲三级视频在线观看| 国产91免费视频| 亚洲色无码专区一区| 免费a级毛片在线观看| 国产成人无码精品久久久久免费| 中文字幕不卡亚洲| 99视频在线精品免费| 亚洲免费在线视频观看| 四虎成人免费观看在线网址 | 久久无码av亚洲精品色午夜| 亚洲一区二区视频在线观看| 国产免费无码AV片在线观看不卡| 中文字幕亚洲第一在线| 最近2019中文字幕mv免费看 | 久久久久久久综合日本亚洲| 91成人免费观看| 亚洲av日韩精品久久久久久a| 亚洲精品亚洲人成在线观看下载| 免费成人高清在线视频| 亚洲a级在线观看| 亚洲精品国产V片在线观看 | 免费无码中文字幕A级毛片| 国产.亚洲.欧洲在线| 亚洲AⅤ无码一区二区三区在线| 久久久精品免费国产四虎| 亚洲AV无码国产精品色| 亚洲欧洲精品成人久久曰影片| 57pao国产成视频免费播放| 激情婷婷成人亚洲综合| 亚洲嫩草影院久久精品| 国产aa免费视频|