String.getBytes()方法中的中文編碼問題

String.getBytes()的問題

String的getBytes()方法是得到一個字串的字節數組,這是衆所周知的。但特別要注意的是,本方法將返回該操作系統默認的編碼格式的字節數組。如果你在使用這個方法時不考慮到這一點,你會發現在一個平臺上運行良好的系統,放到另外一臺機器後會產生意想不到的問題。比如下面的程序,
 

  1. class TestCharset 
  2. {
  3.  
  4.     public static void main(String[] args) 
  5.     {
  6.         new TestCharset().execute();
  7.     }
  8.  
  9.     private void execute() {
  10.         String s = "Hello!你好!";
  11.         
  12.         byte[] bytes = s.getBytes();
  13.  
  14.         System.out.println("bytes lenght is:" + bytes.length);
  15.  
  16.  
  17.     }
  18.  
  19. }


在一箇中文WindowsXP系統下,運行時,結果爲:
bytes lenght is:12

但是如果放到了一個英文的UNIX環境下運行:
$ java TestCharset
bytes lenght is:9

如果你的程序依賴於該結果,將在後續操作中引起問題。爲什麼在一個系統中結果爲12,而在另外一個卻變成了9了呢?上面已經提到了,該方法是和平臺(編碼)相關的。在中文操作系統中,getBytes方法返回的是一個GBK或者GB2312的中文編碼的字節數組,其中中文字符,各佔兩個字節。而在英文平臺中,一般的默認編碼是“ISO-8859-1”,每個字符都只取一個字節(而不管是否非拉丁字符)。
 

Java中的編碼支持


Java是支持多國編碼的,在Java中,字符都是以Unicode進行存儲的,比如,“你”字的Unicode編碼是“4f60”,我們可以通過下面的實驗代碼來驗證:
 

  1. class TestCharset 
  2. {
  3.  
  4.     public static void main(String[] args) 
  5.     {
  6.         char c = '你';
  7.         int i = c;
  8.         System.out.println(c);
  9.         System.out.println(i);
  10.     }
  11.  
  12. }


不管你在任何平臺上執行,都會有相同的輸出:

----------------- output ------------------

20320

20320就是Unicode “4f60”的整數值。其實,你可以反編譯上面的類,可以發現在生成的.class文件中字符“你”(或者其它任何中文字串)本身就是以Unicode編碼進行存儲的:
 

  1.         char c = 'u4F60';
  2.         ... ...


即使你知道了編碼的編碼格式,比如:
javac -encoding GBK TestCharset.java
編譯後生成的.class文件中仍然是以Unicode格式存儲中文字符或字符串的。
 

使用String.getBytes(String charset)方法


所以,爲了避免這種問題,我建議大家都在編碼中使用String.getBytes(String charset)方法。下面我們將從字串分別提取ISO-8859-1和GBK兩種編碼格式的字節數組,看看會有什麼結果:
 

  1. class TestCharset 
  2. {
  3.  
  4.     public static void main(String[] args) 
  5.     {
  6.         new TestCharset().execute();
  7.     }
  8.  
  9.     private void execute() {
  10.         String s = "Hello!你好!";
  11.         
  12.         byte[] bytesISO8859 =null;
  13.         byte[] bytesGBK = null;
  14.  
  15.         try
  16.         {
  17.             bytesISO8859 = s.getBytes("iso-8859-1");
  18.             bytesGBK = s.getBytes("GBK");
  19.         }
  20.         catch (java.io.UnsupportedEncodingException e)
  21.         {
  22.             e.printStackTrace();
  23.         }
  24.  
  25.         System.out.println("--------------   8859 bytes:");
  26.         System.out.println("bytes is:     " + arrayToString(bytesISO8859));
  27.         System.out.println("hex format is:" + encodeHex(bytesISO8859));
  28.         System.out.println();
  29.  
  30.         System.out.println("--------------   GBK bytes:");
  31.         System.out.println("bytes is:     " + arrayToString(bytesGBK));
  32.         System.out.println("hex format is:" + encodeHex(bytesGBK));
  33.  
  34.     }
  35.  
  36.     public static final String encodeHex (byte[] bytes) {
  37.         StringBuffer buff = new StringBuffer(bytes.length * 2);
  38.         String b;
  39.         for (int i=0; i<bytes.length ; i++)
  40.         {
  41.             b = Integer.toHexString(bytes[i]); 
  42.             // byte是兩個字節的,而上面的Integer.toHexString會把字節擴展爲4個字節
  43.             buff.append(b.length() > 2 ? b.substring(6,8) : b); 
  44.             buff.append(" ");
  45.         }
  46.         return buff.toString();
  47.     }
  48.  
  49.     public static final String arrayToString (byte[] bytes) {
  50.         StringBuffer buff = new StringBuffer();
  51.         for (int i=0; i<bytes.length ; i++)
  52.         {
  53.             buff.append(bytes[i] + " ");
  54.         }
  55.         return buff.toString();
  56.     }
  57.  
  58. }

執行上面程序將打印出:
 

  1. --------------
  2.  8859 bytes:
  3. bytes is:     72 101 108 108 111 33 63 63 63
  4. hex format is:48 65 6c 6c 6f 21 3f 3f 3f
  5.  
  6. --------------
  7.  GBK bytes:
  8. bytes is:     72 101 108 108 111 33 -60 -29 -70 -61 -93 -95
  9. hex format is:48 65 6c 6c 6f 21 c4 e3 ba c3 a3 a1


可見,在s中提取的8859-1格式的字節數組長度爲9,中文字符都變成了“63”,ASCII碼爲63的是“?”,一些國外的程序在國內中文環境下運行時, 經常出現亂碼,上面佈滿了“?”,就是因爲編碼沒有進行正確處理的結果。而提取的GBK編碼的字節數組中正確得到了中文字符的GBK編碼。字符“你”“好”“!”的GBK編碼分別是:“c4e3”“bac3”“a3a1”。得到了正確的以GBK編碼的字節數組,以後需要還原爲中文字串時,可以使用下面方法:
new String(byte[] bytes, String charset)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章