string的getbytes()方法是得到一个字串的字节数组,这是众所周知的。但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组。如果你在使用这个方法时不考虑到这一点,你会发现在一个平台上运行.
良好的系统,放到另外一台机器后会产生意想不到的问题。比如下面的程序,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)方法
所以,为了避免这种问题,我建议大家都在编码中使用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("-------------- /n 8859 bytes:"); system.out.println("bytes is: " + arraytostring(bytesiso8859)); system.out.println("hex format is:" + encodehex(bytesiso8859)); system.out.println(); system.out.println("-------------- /n 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(); }}
执行上面程序将打印出:
-------------- 8859 bytes:bytes is: 72 101 108 108 111 33 63 63 63hex format is:48 65 6c 6c 6f 21 3f 3f 3f-------------- gbk bytes:bytes is: 72 101 108 108 111 33 -60 -29 -70 -61 -93 -95hex 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)
闽公网安备 35060202000074号