java中不同的编码方式,一个 汉字占的字节大小不同
iso-8859-1:一个汉字,一个字节
gbk:一个汉字,2个字节
utf-8:一个汉字,3个字节
实例如下
public static void main(String[] args) {
try {
String str="电话费";
System.out.println(str.getBytes("ISO-8859-1").length);
System.out.println(str.getBytes("GBK").length);
System.out.println(str.getBytes("UTF-8").length);
System.out.println();
String str2="电话费dkj";
System.out.println(str2.getBytes("ISO-8859-1").length);
System.out.println(str2.getBytes("GBK").length);
System.out.println(str2.getBytes("UTF-8").length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
输出结果:
3
6
9
6
9
12