原文:http://blog.csdn.net/54powerman/article/details/77575656
作者:54powerman
一直以为,java中任意unicode字符串,可以使用任意字符集转为byte[]再转回来,只要不抛出异常就不会丢失数据,事实证明这是错的。
经过这个实例,也明白了为什么 getBytes()需要捕获异常,虽然有时候它也没有捕获到异常。
言归正传,先看一个实例。
用ISO-8859-1中转UTF-8数据
设想一个场景:
用户A,有一个UTF-8编码的字节流,通过一个接口传递给用户B;
用户B并不知道是什么字符集,他用ISO-8859-1来接收,保存;
在一定的处理流程处理后,把这个字节流交给用户C或者交还给用户A,他们都知道这是UTF-8,他们解码得到的数据,不会丢失。
下面代码验证:
01 | public static void main(String[] args) throws Exception { |
02 | //这是一个unicode字符串,与字符集无关 |
05 | System.out.println( 'unicode字符串:' str1); |
08 | byte [] byteArray1=str1.getBytes( 'UTF-8' ); //这个很安全,UTF-8不会造成数据丢失 |
10 | System.out.println(byteArray1.length); //打印6,没毛病 |
12 | //下面交给另外一个人,他不知道这是UTF-8字节流,因此他当做ISO-8859-1处理 |
14 | //将byteArray1当做一个普通的字节流,按照ISO-8859-1解码为一个unicode字符串 |
15 | String str2= new String(byteArray1, 'ISO-8859-1' ); |
17 | System.out.println( '转成ISO-8859-1会乱码:' str2); |
19 | //将ISO-8859-1编码的unicode字符串转回为byte[] |
20 | byte [] byteArray2=str2.getBytes( 'ISO-8859-1' ); //不会丢失数据 |
25 | String str3= new String(byteArray2, 'UTF-8' ); |
27 | System.out.println( '数据没有丢失:' str3); |
输出:
用GBK中转UTF-8数据
重复前面的流程,将ISO-8859-1 用GBK替换。
只把中间一段改掉:
1 | //将byteArray1当做一个普通的字节流,按照GBK解码为一个unicode字符串 |
2 | String str2= new String(byteArray1, 'GBK' ); |
4 | System.out.println( '转成GBK会乱码:' str2); |
6 | //将GBK编码的unicode字符串转回为byte[] |
7 | byte [] byteArray2=str2.getBytes( 'GBK' ); //数据会不会丢失呢? |
运行结果:
好像没有问题,这就是一个误区。
修改原文字符串重新测试
将两个汉字 “用户” 修改为三个汉字 “用户名” 重新测试。
ISO-8859-1测试结果:
GBK 测试结果:
结论出来了
ISO-8859-1 可以作为中间编码,不会导致数据丢失;
GBK 如果汉字数量为偶数,不会丢失数据,如果汉字数量为奇数,必定会丢失数据。
why?
为什么奇数个汉字GBK会出错
直接对比两种字符集和奇偶字数的情形
重新封装一下前面的逻辑,写一段代码来分析:
01 | public static void demo(String str) throws Exception { |
02 | System.out.println( '原文:' str); |
04 | byte [] utfByte = str.getBytes( 'UTF-8' ); |
05 | System.out.print( 'utf Byte:' ); |
07 | String gbk = new String(utfByte, 'GBK' ); //这里实际上把数据破坏了 |
08 | System.out.println( 'to GBK:' gbk); |
10 | byte [] gbkByte=gbk.getBytes( 'GBK' ); |
11 | String utf = new String(gbkByte, 'UTF-8' ); |
12 | System.out.print( 'gbk Byte:' ); |
14 | System.out.println( 'revert UTF8:' utf); |
15 | System.out.println( '===' ); |
16 | // 如果gbk变成iso-8859-1就没问题 |
19 | public static void printHex( byte [] byteArray) { |
20 | StringBuffer sb = new StringBuffer(); |
21 | for ( byte b : byteArray) { |
22 | sb.append(Integer.toHexString((b >> 4 ) & 0xF )); |
23 | sb.append(Integer.toHexString(b & 0xF )); |
26 | System.out.println(sb.toString()); |
29 | public static void main(String[] args) throws Exception { |
32 | demo(str1, 'UTF-8' , 'ISO-8859-1' ); |
33 | demo(str2, 'UTF-8' , 'ISO-8859-1' ); |
35 | demo(str1, 'UTF-8' , 'GBK' ); |
36 | demo(str2, 'UTF-8' , 'GBK' ); |
输出结果:
02 | UTF- 8 Byte:e5 a7 93 e5 90 8d |
04 | ISO- 8859 - 1 Byte:e5 a7 93 e5 90 8d |
08 | UTF- 8 Byte:e7 94 a8 e6 88 b7 e5 90 8d |
09 | to ISO- 8859 - 1 :用户å |
10 | ISO- 8859 - 1 Byte:e7 94 a8 e6 88 b7 e5 90 8d |
14 | UTF- 8 Byte:e5 a7 93 e5 90 8d |
16 | GBK Byte:e5 a7 93 e5 90 8d |
20 | UTF- 8 Byte:e7 94 a8 e6 88 b7 e5 90 8d |
22 | GBK Byte:e7 94 a8 e6 88 b7 e5 90 3f |
为什么GBK会出错
前三段都没问题,最后一段,奇数个汉字的utf-8字节流转成GBK字符串,再转回来,前面一切正常,最后一个字节,变成了 “0x3f”,即”?”
我们使用”用户名” 三个字来分析,它的UTF-8 的字节流为:
[e7 94 a8] [e6 88 b7] [e5 90 8d]
我们按照三个字节一组分组,他被用户A当做一个整体交给用户B。
用户B由于不知道是什么字符集,他当做GBK处理,因为GBK是双字节编码,如下按照两两一组进行分组:
[e7 94] [a8 e6] [88 b7] [e5 90] [8d ?]
不够了,怎么办?它把 0x8d当做一个未知字符,用一个半角Ascii字符的 “?” 代替,变成了:
[e7 94] [a8 e6] [88 b7] [e5 90] 3f
数据被破坏了。
为什么 ISO-8859-1 没问题
因为 ISO-8859-1 是单字节编码,因此它的分组方案是:
[e7] [94] [a8] [e6] [88] [b7] [e5] [90] [8d]
因此中间不做任何操作,交回个用户A的时候,数据没有变化。
关于Unicode编码
因为UTF-16 区分大小端,严格讲:unicode==UTF16BE。
view sourceprint?
1 | public static void main(String[] args) throws Exception { |
3 | printHex(str.getBytes( 'UNICODE' )); |
4 | printHex(str.getBytes( 'UTF-16LE' )); |
5 | printHex(str.getBytes( 'UTF-16BE' )); |
运行结果:
其中 “fe ff” 为大端消息头,同理,小端消息头为 “ff fe”。
小结
作为中间转存方案,ISO-8859-1 是安全的。
UTF-8 字节流,用GBK字符集中转是不安全的;反过来也是同样的道理。
01 | byte [] utfByte = str.getBytes( 'UTF-8' ); |
02 | String gbk = new String(utfByte, 'GBK' ); |
03 | 这是错误的用法,虽然在ISO- 8859 - 1 时并没报错。 |
05 | 首先, byte [] utfByte = str.getBytes( 'UTF-8' ); |
06 | 执行完成之后,utfByte 已经很明确,这是utf- 8 格式的字节流; |
08 | 然后,gbk = new String(utfByte, 'GBK' ), |
09 | 对utf- 8 的字节流使用gbk解码,这是不合规矩的。 |
11 | 就好比一个美国人说一段英语,让一个不懂英文又不会学舌的日本人听,然后传递消息给另一个美国人。 |
15 | 因为它只认识一个一个的字节,就相当于是一个录音机。我管你说的什么鬼话连篇,过去直接播放就可以了。 |
getBytes() 是会丢失数据的操作,而且不一定会抛异常。
unicode是安全的,因为他是java使用的标准类型,跨平台无差异。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。