String sgbk = "aB 1234567";
System.out.println(sgbk.length()); // 10
System.out.println(sgbk.getBytes("GBK").length); // 10
System.out.println(sgbk.getBytes("UTF-8").length); // 10
String sgbk = "中国移动通信 a1"; // 中文之后有个空格
System.out.println(sgbk.length()); // 9
System.out.println(sgbk.getBytes("GBK").length); // 15,一个中文占两个字节
System.out.println(sgbk.getBytes("UTF-8").length); // 21,一个中文占三个字节
原理:
已unicode为中介
utf-8 ——》unicode——》gbk
gbk ——》unicode——》utf-8
//utf-8转gbk
String clientStr = new String(str.getBytes("GBK"), "GBK");
//gbk转utf-8
String clientStr = new String(str.getBytes("UTF-8"), "UTF-8");