日期:2014-05-20  浏览次数:20701 次

让人吐血的Eclipse控制台编码问题!!!
求救赎~~

先看代码:
Java code
import java.io.IOException;

public class Chinese {
    public static void main(String[] args) throws IOException {
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
        System.out.println(Integer.toHexString(System.in.read()));
    }
}



在Eclipse控制台输入“中国”(UTF-8:bf e4 b8 ad e5 9b bd,¿ä¸­å?½),竟然输出了这样的结果:
Java code
c3
a4
c2
b8
c2
ad
c3
a5
e2
80
ba
c2
bd
d
a



目测,是将“中国”二字的UTF-8字节码(¿ä¸­å?½,e4 b8 ad e5 9b bd),再进行了一次UTF-8编码(¿ä¸­å›½,c2 bf c3 a4 c2 b8 c2 ad c3 a5 e2 80 ba c2 bd)。

谁能告诉我这是为什么以及怎么解决吗?(PS:Eclipse workspace 编码 UTF-8)





------解决方案--------------------
没道理十六进制数会是乱码啊。应该是0~F之间的字符才对。楼主自己写个字节向十六进制转换的转换函数吧。

PS:控制台编码,设置的貌似是输出编码,至于输入编码,貌似是系统默认编码,一般是GB2312。