Java 关于中文乱码问题的解决方案与经验 (1)
2005-02-17 15:18
956 查看
一、字节和unicode
Java内核是unicode的,就连class文件也是,但是很多媒体,包括文件/流的保存方式是使用字节流的。因此Java要对这些字节流经行转化。char是unicode的,而byte是字节。Java中byte/char互转的函数在sun.io的包中间有。其中ByteToCharConverter类是中调度,可以用来告诉你,你用的convertor。其中两个很常用的静态函数是:
如果你不指定converter,则系统会自动使用当前的encoding,gb平台上用gbk,en平台上用8859_1。
如果代码改为:
结果将又是什么?
这就要根据平台的编码而定。
结果将又是什么?还是根据平台的编码而定。
很多中文问题就是从这两个最简单的类派生出来的。而却有很多类不直接支持把encoding输入,这给我们带来诸多不便。很多程序难得用encoding了,直接用default的encoding,这就给我们移植带来了很多困难。
二、utf-8
utf-8是和unicode一一对应的,其实现很简单:
大多数情况是只使用到16位以下的unicode:
由于只有两位我们按照两位的编码来排,但是我们发现这行不通,因为第7位不是0因此,返回"?"
三、string和byte[]
string其实核心是char[],然而要把byte转化成string,必须经过编码。string.length()其实就是char数组的长度,如果使用不同的编码,很可能会错分,造成散字和乱码。例如:
如果encoding=8859_1,会有两个字,但是encoding=gb2312只有一个字这个问题在处理分页是经常发生。
四、Reader,Writer / InputStream,OutputStream
Reader和Writer核心是char,InputStream和OutputStream核心是byte。但是Reader和Writer的主要目的是要把char读/写InputStream/OutputStream。例如:
结果是什么?是"你"。如果encoding ="8859_1",结果是什么?"??"两个字符,表示不认识。反过来的例子自己做。
Java内核是unicode的,就连class文件也是,但是很多媒体,包括文件/流的保存方式是使用字节流的。因此Java要对这些字节流经行转化。char是unicode的,而byte是字节。Java中byte/char互转的函数在sun.io的包中间有。其中ByteToCharConverter类是中调度,可以用来告诉你,你用的convertor。其中两个很常用的静态函数是:
public static ByteToCharConverter getDefault(); public static ByteToCharConverter getConverter(String encoding); |
byte ——〉char: "你"的gb码是:0xc4e3 ,unicode是0x4f60 String encoding = "gb2312"; byte b[] = {(byte)'/u00c4',(byte)'/u00e3'}; ByteToCharConverter converter = ByteToCharConverter.getConverter(encoding); char c[] = converter.convertAll(b); for (int i = 0; i < c.length; i++) { System.out.println(Integer.toHexString(c[i])); } 结果是什么?0x4f60 如果encoding ="8859_1",结果又是什么?0x00c4,0x00e3 |
byte b[] = {(byte)'/u00c4',(byte)'/u00e3'}; ByteToCharConverter converter = ByteToCharConverter. getDefault(); char c[] = converter.convertAll(b); for (int i = 0; i < c.length; i++) { System.out.println(Integer.toHexString(c[i])); } |
这就要根据平台的编码而定。
char ——〉byte: String encoding = "gb2312"; char c[] = {'/u4f60'}; CharToByteConverter converter = CharToByteConverter.getConverter(encoding); byte b[] = converter.convertAll(c); for (int i = 0; i < b.length; i++) { System.out.println(Integer.toHexString(b[i])); } 结果是什么?0x00c4,0x00e3 如果encoding ="8859_1",结果又是什么?0x3f 如果代码改为 String encoding = "gb2312"; char c[] = {'/u4f60'}; CharToByteConverter converter = CharToByteConverter.getDefault(); byte b[] = converter.convertAll(c); for (int i = 0; i < b.length; i++) { System.out.println(Integer.toHexString(b[i])); } |
很多中文问题就是从这两个最简单的类派生出来的。而却有很多类不直接支持把encoding输入,这给我们带来诸多不便。很多程序难得用encoding了,直接用default的encoding,这就给我们移植带来了很多困难。
二、utf-8
utf-8是和unicode一一对应的,其实现很简单:
7位的unicode: 0 _ _ _ _ _ _ _ 11位的unicode: 1 1 0 _ _ _ _ _ 1 0 _ _ _ _ _ _ 16位的unicode: 1 1 1 0 _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ 21位的unicode: 1 1 1 1 0 _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ |
"你"的gb码是:0xc4e3 ,unicode是0x4f60 0xc4e3的二进制: 1100 ,0100 ,1110 ,0011 |
0x4f60的二进制: 0100 ,1111 ,0110 ,0000 我们用utf-8补齐,变成: 1110 ,0100 ,1011 ,1101 ,1010 ,0000 e4--bd-- a0 于是返回:0xe4,0xbd,0xa0。 |
string其实核心是char[],然而要把byte转化成string,必须经过编码。string.length()其实就是char数组的长度,如果使用不同的编码,很可能会错分,造成散字和乱码。例如:
String encoding = “”; byte [] b={(byte)'/u00c4',(byte)'/u00e3'}; String str=new String(b,encoding); |
四、Reader,Writer / InputStream,OutputStream
Reader和Writer核心是char,InputStream和OutputStream核心是byte。但是Reader和Writer的主要目的是要把char读/写InputStream/OutputStream。例如:
文件test.txt只有一个"你"字,0xc4,0xe3 String encoding = "gb2312"; InputStreamReader reader = new InputStreamReader(new FileInputStream( "text.txt"), encoding); char c[] = new char[10]; int length = reader.read(c); for (int i = 0; i < length; i++) { System.out.println(c[i]); } |
相关文章推荐
- Java 关于中文乱码问题的解决方案与经验
- Java 关于中文乱码问题的解决方案与经验(转)
- Java 关于中文乱码问题的解决方案与经验
- Java 关于中文乱码问题的解决方案与经验
- JAVA关于中文乱码问题的解决方案与经验
- Java 关于中文乱码问题的解决方案与经验
- 关于java中文乱码问题一些解决方案和经验
- Java 关于中文乱码问题的解决方案与经验【转载】
- Java 关于中文乱码问题的解决方案与经验 (1)
- Java关于中文乱码问题的解决方案与经验
- Java 关于中文乱码问题的解决方案与经验 (2)
- 转载:Java 关于中文乱码问题的解决方案与经验
- Java 关于中文乱码问题的解决方案与经验 (2)
- Java 关于中文乱码问题的解决方案与经验
- 关于java中文乱码问题一些解决方案和经验
- Java关于中文乱码问题的解决方案与经验
- Java关于中文乱码问题的解决方案与经验
- Java 关于中文乱码问题的解决方案与原理
- 一些关于中文乱码问题的一些解决方案和经验和大家分享!
- 关于中文乱码问题的一些解决方案和经验