解決GBK字符轉UTF-8亂碼問題


gbk轉utf-8,奇數中文亂碼。

一、亂碼的原因

gbk的中文編碼是一個漢字用【2】個字節表示,例如漢字“內部”的gbk編碼16進制的顯示為c4 da b2 bf

utf-8的中文編碼是一個漢字用【3】個字節表示,例如漢字“內部”的utf-8編碼16進制的顯示為e5 86 85 e9 83 a8

很顯然,gbk是無法直接轉換成utf-8,少字節變為多字節

二、轉換的辦法

1.首先將gbk字符串getBytes()得到兩個原始字節,轉換成二進制字符流,共16位。

2.根據UTF-8的漢字編碼規則,首字節以1110開頭,次字節以10開頭,第3字節以10開頭。在原始的2進制字符串中插入標志位。最終的長度從16--->16+3+2+2=24。

3.轉換完成

 

通過以下方法將GBK字符轉成UTF-8編碼格式的byte【】數組

  1. package test;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. public class TestEncoder {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) throws Exception {  
  11.         String gbk = "iteye問答頻道編碼轉換問題";  
  12.           
  13.         String iso = new String(gbk.getBytes("UTF-8"),"ISO-8859-1");   
  14.   
  15.         System.out.println(iso);  
  16.           
  17.         String utf8 = new String(iso.getBytes("ISO-8859-1"),"UTF-8");  
  18.         System.out.println(utf8);  
  19.           
  20.         System.out.println(getUTF8StringFromGBKString(gbk));  
  21.     }  
  22.   
  23.     public static String getUTF8StringFromGBKString(String gbkStr) {  
  24.         try {  
  25.             return new String(getUTF8BytesFromGBKString(gbkStr), "UTF-8");  
  26.         } catch (UnsupportedEncodingException e) {  
  27.             throw new InternalError();  
  28.         }  
  29.     }  
  30.       
  31.     public static byte[] getUTF8BytesFromGBKString(String gbkStr) {  
  32.         int n = gbkStr.length();  
  33.         byte[] utfBytes = new byte[3 * n];  
  34.         int k = 0;  
  35.         for (int i = 0; i < n; i++) {  
  36.             int m = gbkStr.charAt(i);  
  37.             if (m < 128 && m >= 0) {  
  38.                 utfBytes[k++] = (byte) m;  
  39.                 continue;  
  40.             }  
  41.             utfBytes[k++] = (byte) (0xe0 | (m >> 12));  
  42.             utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));  
  43.             utfBytes[k++] = (byte) (0x80 | (m & 0x3f));  
  44.         }  
  45.         if (k < utfBytes.length) {  
  46.             byte[] tmp = new byte[k];  
  47.             System.arraycopy(utfBytes, 0, tmp, 0, k);  
  48.             return tmp;  
  49.         }  
  50.         return utfBytes;  
  51.     }  

或者:

  1. public static void gbk2Utf() throws UnsupportedEncodingException {  
  2.     String gbk = "我來了";  
  3.     char[] c = gbk.toCharArray();  
  4.     byte[] fullByte = new byte[3*c.length];  
  5.     for (int i=0; i<c.length; i++) {  
  6.         String binary = Integer.toBinaryString(c[i]);  
  7.         StringBuffer sb = new StringBuffer();  
  8.         int len = 16 - binary.length();  
  9.         //前面補零  
  10.         for(int j=0; j<len; j++){  
  11.                 sb.append("0");  
  12.             }  
  13.         sb.append(binary);  
  14.         //增加位,達到到24位3個字節  
  15.         sb.insert(0, "1110");  
  16.             sb.insert(8, "10");  
  17.             sb.insert(16, "10");  
  18.             fullByte[i*3] = Integer.valueOf(sb.substring(0, 8), 2).byteValue();//二進制字符串創建整型  
  19.             fullByte[i*3+1] = Integer.valueOf(sb.substring(8, 16), 2).byteValue();  
  20.             fullByte[i*3+2] = Integer.valueOf(sb.substring(16, 24), 2).byteValue();  
  21.     }  
  22.     //模擬UTF-8編碼的網站顯示  
  23.     System.out.println(new String(fullByte,"UTF-8"));  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM