龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

String.getBytes()方法中的中文编码问题[组图]

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
String的getBytes()方法是得到一个字串的字节数组,这是众所周知的。但非凡要注重的是,本方法将返回该操作系统默认的编码格式的字节数组。假如你在使用这个方法时不考虑到这一点,

  String的getBytes()方法是得到一个字串的字节数组,这是众所周知的。但非凡要注重的是,本方法将返回该操作系统默认的编码格式的字节数组。假如你在使用这个方法时不考虑到这一点,你会发现在一个平台上运行.

<!-- frame contents -->
<!-- /frame contents -->

  良好的系统,放到另外一台机器后会产生意想不到的问题。比如下面的程序,class TestCharset { public static void main(String[] args)  {  new TestCharset().execute(); } private void execute() {  String s = "Hello!你好!";    byte[] bytes = s.getBytes();  System.out.println("bytes lenght is:" + bytes.length); }}
  
  在一个中文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”,我们可以通过下面的实验代码来验证:

  class TestCharset { public static void main(String[] args)  {  char c = '你';  int i = c;  System.out.println(c);  System.out.println(i); }}
  
  不管你在任何平台上执行,都会有相同的输出:
  ----------------- output ------------------
  你
  20320

  20320就是Unicode “4f60”的整数值。其实,你可以反编译上面的类,可以发现在生成的.class文件中字符“你”(或者其它任何中文字串)本身就是以Unicode编码进行存储的:

          char c = 'u4F60';        ... ...
   进入讨论组讨论。


  即使你知道了编码的编码格式,比如:
  javac -encoding GBK TestCharset.java
  编译后生成的.class文件中仍然是以Unicode格式存储中文字符或字符串的。

  使用String.getBytes(String charset)方法

<!-- frame contents -->
<!-- /frame contents -->

  所以,为了避免这种问题,我建议大家都在编码中使用String.getBytes(String charset)方法。下面我们将从字串分别提取ISO-8859-1和GBK两种编码格式的字节数组,看看会有什么结果:

  class TestCharset { public static void main(String[] args)  {  new TestCharset().execute(); } private void execute() {  String s = "Hello!你好!";    byte[] bytesISO8859 =null;  byte[] bytesGBK = null;  try  {   bytesISO8859 = s.getBytes("iso-8859-1");   bytesGBK = s.getBytes("GBK");  }  catch (java.io.UnsupportedEncodingException e)  {   e.printStackTrace();  }  System.out.println("-------------- 8859 bytes:");  System.out.println("bytes is:     " + arrayToString(bytesISO8859));  System.out.println("hex format is:" + encodeHex(bytesISO8859));  System.out.println();  System.out.println("-------------- GBK bytes:");  System.out.println("bytes is:     " + arrayToString(bytesGBK));  System.out.println("hex format is:" + encodeHex(bytesGBK)); } public static final String encodeHex (byte[] bytes) {  StringBuffer buff = new StringBuffer(bytes.length * 2);  String b;  for (int i=0; i<bytes.length ; i++)  {   b = Integer.toHexString(bytes[i]);    // byte是两个字节的,而上面的Integer.toHexString会把字节扩展为4个字节   buff.append(b.length() > 2 ? b.substring(6,8) : b);    buff.append(" ");  }  return buff.toString(); } public static final String arrayToString (byte[] bytes) {  StringBuffer buff = new StringBuffer();  for (int i=0; i<bytes.length ; i++)  {   buff.append(bytes[i] + " ");  }  return buff.toString(); }}
  
  

精彩图集

赞助商链接