用java程式將GBK字元轉成UTF-8編碼格式)

來源:互聯網
上載者:User

UTF-8 編碼是一種被廣泛應用的編碼,這種編碼致力於把全球的語言納入一個統一的編碼,
目前已經將幾種亞洲語言納入。UTF 代表 UCS Transformation Format.
UTF-8 採用變長度位元組來表示字元,理論上最多可以到 6 個位元組長度。
UTF-8 編碼相容了 ASC II(0-127), 也就是說 UTF-8 對於 ASC II 字元的編碼是和 ASC II 一樣的。
對於超過一個位元組長度的字元,才用以下編碼規範:
左邊第一個位元組1的個數表示這個字元編碼位元組的位元,
例如
單位元組編碼樣式為:0xxxxxxx
兩位位元組字元編碼樣式為:110xxxxx 10xxxxxx;
三位位元組字元的編碼樣式為:1110xxxx 10xxxxxx 10xxxxxx.;
以此類推,六位位元組字元的編碼樣式為:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx。
 
xxx 的值由字元編碼的二進位表示的位填入。只用最短的那個足夠表達一個字元編碼的多位元組串。
實際表示ASCII字元的UNICODE字元,將會編碼成1個位元組,並且UTF-8表示與ASCII字元表示是一樣的。所有其他的UNCODE字元轉化成UTF-8將需要至少2個位元組。每個位元組由一個換碼序列開始。第一個位元組由唯一的換碼序列,由n位1加一位0組成。n位1表示字元編碼所需的位元組數。
UTF-8每個編碼字元都不可能以“10”開頭,“10”是以串連符的形式出現在後面的編碼位元組開頭。因此UTF-8編碼在儲存和傳輸時是不容易出錯的。
例如:
Unicode 字元: 00 A9(著作權符號) = 1010 1001,
UTF-8 編碼為:11000010 10101001 = 0x C2 0xA9;
字元 22 60 (不等於符號) = 0010 0010 0110 0000,
UTF-8 編碼為:11100010 10001001 10100000 = 0xE2 0x89 0xA0
 
package com.lang.string;

public class ConverFromGBKToUTF8 {
 public static void main(String[] args){
 
  try {
  
         ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();
         byte [] fullByte = convert.gbk2utf8(chenese);
         String fullStr = new String(fullByte, "UTF-8");
         System.out.println("string from GBK to UTF-8 byte:  " + fullStr);

     } catch (Exception e) {
      e.printStackTrace();
     }
 }
 
 public byte[] gbk2utf8(String chenese){
  char c[] = chenese.toCharArray();
        byte [] fullByte =new byte[3*c.length];
        for(int i=0; i<c.length; i++){
         int m = (int)c[i];
         String word = Integer.toBinaryString(m);
//         System.out.println(word);
        
         StringBuffer sb = new StringBuffer();
         int len = 16 - word.length();
         //補零
         for(int j=0; j<len; j++){
          sb.append("0");
         }
         sb.append(word);
         sb.insert(0, "1110");
         sb.insert(8, "10");
         sb.insert(16, "10");
        
//         System.out.println(sb.toString());
        
         String s1 = sb.substring(0, 8);         
         String s2 = sb.substring(8, 16);         
         String s3 = sb.substring(16);
        
         byte b0 = Integer.valueOf(s1, 2).byteValue();
         byte b1 = Integer.valueOf(s2, 2).byteValue();
         byte b2 = Integer.valueOf(s3, 2).byteValue();
         byte[] bf = new byte[3];
         bf[0] = b0;
         fullByte[i*3] = bf[0];
         bf[1] = b1;
         fullByte[i*3+1] = bf[1];
         bf[2] = b2;
         fullByte[i*3+2] = bf[2];
        
        }
        return fullByte;
 }
}

UTF-8的編碼原理和特性:

U+0000~U+007E 1 _ _ _ _ _ _ _ (7bits)

U+0080~U+07FF 1 1 0_ _ _ _ _ 1 0_ _ _ _ _ _ (11bits)

U+0800~U+FFFF 1 1 1 0 _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ (16bits)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.