使用的开发环境是jbuilder 9,工程设置中ecoding选择utf8,代码如下:
String str = "中国";
byte []buf = new byte[str.getBytes().length];
buf = str.getBytes("GBK");
System.out.println(str);
String str2 = new String(buf,"GBK");
System.out.println(str2);
输出结果为乱码,请高手点评。
如果工程设置中ecoding使用GBK,则可以正确显示,也请说明原因。
String str = "中国";
byte []buf = new byte[str.getBytes().length];
buf = str.getBytes("GBK");
System.out.println(str);
String str2 = new String(buf,"GBK");
System.out.println(str2);
输出结果为乱码,请高手点评。
如果工程设置中ecoding使用GBK,则可以正确显示,也请说明原因。
utf8使用的1-3个字节对字符进行编码,对在ACSII中出现的字符编码规制没变,而对其他的字符则使用2-3个字节进行编码,
而GBK使用2个字节对字符进行编码
如果你使用utf8对程序进行编译,在使用GBK存取字符,出现乱码是必然的