在String中使用
String s = "I am 慕司";
try {
//由字符转变为字节,此为编码
byte[] b = s.getBytes("GB2312");
//由字节变为字符,此为解码
String n = new String(b,"GBK");
System.out.println(n);
}catch(Exception e) {
}
}
GBK是兼容GB2312的,所以显示正常
String s = "I am 慕司";
try {
//由字符转变为字节,此为编码
byte[] b = s.getBytes("GB2312");
//由字节变为字符,此为解码
String n = new String(b,"UTF-8");
System.out.println(n);
}catch(Exception e) {
}
}
UTF-8不兼容GB2312等,所i显示不正常
String s = "I am 慕司";
try {
//由字符转变为字节,此为编码
byte[] b = s.getBytes("ISO8859-1");
//由字节变为字符,此为解码
String n = new String(b,"ISO8859-1");
System.out.println(n);
}catch(Exception e) {
}
}
ISO8859-1不支持汉字,所以出现乱码