posts - 325,  comments - 25,  trackbacks - 0

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

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

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 康马县| 法库县| 元江| 张家界市| 温泉县| 沁水县| 车致| 叶城县| 商洛市| 两当县| 灌阳县| 辉县市| 永靖县| 沈丘县| 图木舒克市| 两当县| 固镇县| 镇雄县| 新安县| 丽江市| 呼伦贝尔市| 花莲县| 固镇县| 广饶县| 青岛市| 安康市| 安国市| 北宁市| 徐汇区| 石首市| 垦利县| 桂东县| 汨罗市| 青川县| 贵定县| 绥宁县| 金坛市| 湄潭县| 乐陵市| 杭锦后旗| 平凉市|