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

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 农安县| 金门县| 永靖县| 浦城县| 乾安县| 奉节县| 普宁市| 格尔木市| 闵行区| 靖安县| 西华县| 南召县| 越西县| 如东县| 土默特右旗| 临武县| 开阳县| 奉节县| 景德镇市| 凤凰县| 将乐县| 清苑县| 香格里拉县| 云梦县| 卓资县| 武清区| 台北县| 泰安市| 高安市| 习水县| 开鲁县| 河南省| 公主岭市| 成都市| 包头市| 建昌县| 公安县| 得荣县| 噶尔县| 墨竹工卡县| 伽师县|