分享

【Java 基础专题】编码与乱码(02)---String的getBytes([encod...

 昵称465563 2010-11-15
【Java 基础专题】编码与乱码(02)---String的getBytes([encoding])方法测试
cobra - 2010-2-22 23:50:00
上一篇:【Java 基础专题】编码与乱码(01)---编码基础
  1. package example.encoding;

  2. import java.io.UnsupportedEncodingException;

  3. /** *//**
  4. * The Class GetBytesTest.
  5. */
  6. public class GetBytesTest {

  7.     /** *//**
  8.     * The main method.
  9.     *
  10.     * @param args the arguments
  11.     */
  12.     public static void main(String args[]) {
  13.        
  14.         String content = "中文";
  15.         String defaultEncoding = System.getProperty("file.encoding");
  16.         String defaultLnaguage = System.getProperty("user.language");
  17.         System.out.println("System default encoding --- " + defaultEncoding);
  18.         System.out.println("System default language --- " + defaultLnaguage);

  19.         GetBytesTest tester = new GetBytesTest();

  20.         byte[] defaultBytes = tester.getBytesWithDefaultEncoding(content);
  21.         tester.printBytes(defaultBytes);

  22.         byte[] iso8859Bytes = tester.getBytesWithGivenEncoding(content,
  23.                 "ISO-8859-1");
  24.         tester.printBytes(iso8859Bytes);

  25.         byte[] gbkBytes = tester.getBytesWithGivenEncoding(content, "GBK");
  26.         tester.printBytes(gbkBytes);

  27.         byte[] utfBytes = tester.getBytesWithGivenEncoding(content, "UTF-8");
  28.         tester.printBytes(utfBytes);

  29.     }

  30.     /** *//**
  31.     * Gets the bytes with default encoding.
  32.     *
  33.     * @param content the content
  34.     *
  35.     * @return the bytes with default encoding
  36.     */
  37.     public byte[] getBytesWithDefaultEncoding(String content) {
  38.         System.out.println("\nEncode with default encoding\n");
  39.         byte[] bytes = content.getBytes();
  40.         return bytes;
  41.     }

  42.     /** *//**
  43.     * Gets the bytes with given encoding.
  44.     *
  45.     * @param content the content
  46.     * @param encoding the encoding
  47.     *
  48.     * @return the bytes with given encoding
  49.     */
  50.     public byte[] getBytesWithGivenEncoding(String content, String encoding) {
  51.         System.out.println("\nEncode with given encoding : " + encoding + "\n");
  52.         try {
  53.             byte[] bytes = content.getBytes(encoding);
  54.             return bytes;
  55.         } catch (UnsupportedEncodingException e) {
  56.             e.printStackTrace();
  57.             return null;
  58.         }
  59.     }

  60.     /** *//**
  61.     * Prints the bytes.
  62.     *
  63.     * @param bytes the bytes
  64.     */
  65.     public void printBytes(byte[] bytes) {
  66.         for (int i = 0; i < bytes.length; i++) {
  67.             System.out.print(" byte[" + i + "] = " + bytes);
  68.             System.out
  69.                     .println(" hex string = " + Integer.toHexString(bytes));
  70.         }
  71.     }

  72. }
复制代码
【1】在中文平台下,测试结果如下:
System default encoding --- GBK
System default language --- zh

Encode with default encoding

byte[0] = -42 hex string = ffffffd6
byte[1] = -48 hex string = ffffffd0
byte[2] = -50 hex string = ffffffce
byte[3] = -60 hex string = ffffffc4

Encode with given encoding : ISO-8859-1

byte[0] = 63 hex string = 3f
byte[1] = 63 hex string = 3f

Encode with given encoding : GBK

byte[0] = -42 hex string = ffffffd6
byte[1] = -48 hex string = ffffffd0
byte[2] = -50 hex string = ffffffce
byte[3] = -60 hex string = ffffffc4

Encode with given encoding : UTF-8

byte[0] = -28 hex string = ffffffe4
byte[1] = -72 hex string = ffffffb8
byte[2] = -83 hex string = ffffffad
byte[3] = -26 hex string = ffffffe6
byte[4] = -106 hex string = ffffff96
byte[5] = -121 hex string = ffffff87

【2】在英文平台下,测试结果如下:
System default encoding --- Cp1252
System default language --- en

Encode with default encoding

byte[0] = 63 hex string = 3f
byte[1] = 63 hex string = 3f

Encode with given encoding : ISO-8859-1

byte[0] = 63 hex string = 3f
byte[1] = 63 hex string = 3f

Encode with given encoding : GBK

byte[0] = -42 hex string = ffffffd6
byte[1] = -48 hex string = ffffffd0
byte[2] = -50 hex string = ffffffce
byte[3] = -60 hex string = ffffffc4

Encode with given encoding : UTF-8

byte[0] = -28 hex string = ffffffe4
byte[1] = -72 hex string = ffffffb8
byte[2] = -83 hex string = ffffffad
byte[3] = -26 hex string = ffffffe6
byte[4] = -106 hex string = ffffff96
byte[5] = -121 hex string = ffffff87

【结论】

getBytes()、getBytes(encoding)函数的作用是使用系统默认或者指定的字符集编码方式,将字符串编码成字节数组。

在中文平台下,默认的字符集编码是GBK,此时如果使用getBytes()或者getBytes("GBK"),则按照GBK的编码规则将每个中文字符用2个byte表示。所以我们看到"中文"最终GBK编码结果就是: -42 -48 -50 -60 。-42和-48代表了"中"字,而"-50"和"-60"则代表了"文"字。

在中文平台下,如果指定的字符集编码是UTF-8,那么按照UTF-8对中文的编码规则:每个中文用3个字节表示,那么"中文"这两个字符最终被编码成:-28 -72 -83、-26 -106 -121两组。每3个字节代表一个中文字符。

在中文平台下,如果指定的字符集编码是ISO-8859-1,由于此字符集是单字节编码,所以使用getBytes("ISO-8859-1") 时,每个字符只取一个字节,每个汉字只取到了一半的字符。另外一半的字节丢失了。由于这一半的字符在字符集中找不到对应的字符,所以默认使用编码63代替,也就是?。

在英文平台下,默认的字符集编码是Cp1252(类似于ISO-8859-1),如果使用GBK、UTF-8进行编码,得到的字节数组依然是正确的 (GBK4个字节,UTF-8是6个字节)。因为在JVM内部是以Unicode存储字符串的,使用getBytes(encoding)会让JVM进行一次Unicode到指定编码之间的转换。对于GBK,JVM依然会转换成4个字节,对于UTF-8,JVM依然会转换成6个字节。但是对于ISO- 8859-1,则由于无法转换(2个字节--->1个字节,截取了一半的字节),所以转换后的结果是错误的。

相同的平台下,同一个中文字符,在不同的编码方式下,得到的是完全不同的字节数组。这些字节数组有可能是正确的(只要该字符集支持中文),也可能是完全错误的(该字符集不支持中文)。

记住:

不要轻易地使用或滥用String类的getBytes(encoding)方法,更要尽量避免使用getBytes()方法。因为这个方法是平台依赖的,在平台不可预知的情况下完全可能得到不同的结果。如果一定要进行字节编码,则用户要确保encoding的方法就是当初字符串输入时的 encoding。(文/Paul Lin

下一篇:【Java 基础专题】编码与乱码(03)----String的toCharArray()方法测试

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多