posts - 325,  comments - 25,  trackbacks - 0

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

          反之使用new String根據(jù)byte數(shù)組構(gòu)造字符串對(duì)象時(shí),需要根據(jù)byte[]數(shù)組所使用的字符編碼進(jìn)行相應(yīng)轉(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 長(zhǎng)春語(yǔ)林科技 閱讀(3463) 評(píng)論(0)  編輯  收藏 所屬分類: android
          <2011年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

           

          長(zhǎng)春語(yǔ)林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 普兰县| 叶城县| 延庆县| 广元市| 广水市| 永泰县| 刚察县| 东方市| 东丰县| 牟定县| 建平县| 耒阳市| 古浪县| 顺昌县| 紫金县| 滦平县| 黑龙江省| 广饶县| 滕州市| 荆门市| 镇原县| 清河县| 莲花县| 胶南市| 朝阳市| 和龙市| 龙井市| 阿克苏市| 万源市| 芒康县| 盈江县| 江都市| 洪洞县| 信阳市| 阿拉善盟| 台山市| 左权县| 榆林市| 顺昌县| 湖南省| 乌兰浩特市|