站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题String_getBytes()方法中的中文编码问题

String_getBytes()方法中的中文编码问题

摘要: String_getBytes()方法中的中文编码问题

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="684" height="20">

String的getBytes()方法是得到一个字串的字节数组,这是众所周知的。但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组。如果你在使用这个方法时不考虑到这一点,你会发现在一个平台上运行良好的系统,放到另外一台机器后会产生意想不到的问题。比如下面的程序: <table width="675" border="0"> <tr> <td width="394">

  1. class TestCharset
  2. {
  3. public static void main(String[] args)
  4. {
  5. new TestCharset().execute();
  6. }
  7. private void execute() {
  8. String s = "Hello!你好!";
  9. byte[] bytes = s.getBytes();
  10. System.out.println("bytes lenght is:" + bytes.length);
  11. }
  12. }
</td> <td width="271"> </td> </tr> </table>

在一个中文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. public static void main(String[] args)
  4. {
  5. char c = '你';
  6. int i = c;
  7. System.out.println(c);
  8. System.out.println(i);
  9. }
  10. }

不管你在任何平台上执行,都会有相同的输出:

----------------- 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. public static void main(String[] args)
  4. {
  5. new TestCharset().execute();
  6. }
  7. private void execute() {
  8. String s = "Hello!你好!";
  9. byte[] bytesISO8859 =null;
  10. byte[] bytesGBK = null;
  11. try
  12. {
  13. bytesISO8859 = s.getBytes("iso-8859-1");
  14. bytesGBK = s.getBytes("GBK");
  15. }
  16. catch (java.io.UnsupportedEncodingException e)
  17. {
  18. e.printStackTrace();
  19. }
  20. System.out.println("-------------- \n 8859 bytes:");
  21. System.out.println("bytes is: " + arrayToString(bytesISO8859));
  22. System.out.println("hex format is:" + encodeHex(bytesISO8859));
  23. System.out.println();
  24. System.out.println("-------------- \n GBK bytes:");
  25. System.out.println("bytes is: " + arrayToString(bytesGBK));
  26. System.out.println("hex format is:" + encodeHex(bytesGBK));
  27. }
  28. public static final String encodeHex (byte[] bytes) {
  29. StringBuffer buff = new StringBuffer(bytes.length * 2);
  30. String b;
  31. for (int i=0; ilength ; i++)
  32. {
  33. b = Integer.toHexString(bytes[i]);
  34. // byte是两个字节的,而上面的Integer.toHexString会把字节扩展为4个字节
  35. buff.append(b.length() > 2 ? b.substring(6,8) : b);
  36. buff.append(" ");
  37. }
  38. return buff.toString();
  39. }
  40. public static final String arrayToString (byte[] bytes) {
  41. StringBuffer buff = new StringBuffer();
  42. for (int i=0; ilength ; i++)
  43. {
  44. buff.append(bytes[i] + " ");
  45. }
  46. return buff.toString();
  47. }
  48. }
执行上面程序将打印出:
  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. GBK bytes:
  7. bytes is: 72 101 108 108 111 33 -60 -29 -70 -61 -93 -95
  8. 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)

文章
↑返回目录
前一篇: 三重DES加密程序
后一篇: Java中堆和栈的区别