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