编码
变长Unicode字符
UTF-8
字母 1个字节
汉字 3个字节
public class Encode {
public static void main(String[] args) {
String src="这是我的abc";
byte[] bytes = src.getBytes();
System.out.println(bytes.length);
}
}
GBK
字母 1个字节
汉字 2个字节
import java.io.UnsupportedEncodingException;
public class Encode {
public static void main(String[] args) throws UnsupportedEncodingException {
String src="这是我的abc";
byte[] bytes = src.getBytes();
bytes = src.getBytes("GBK");
System.out.println(bytes.length);
}
}
解码
import java.io.UnsupportedEncodingException;
public class Encode {
public static void main(String[] args) throws UnsupportedEncodingException {
String src="这是我的abc";
byte[] bytes = src.getBytes();
String s;
//解码
s=new String(bytes,0,bytes.length,"utf-8");
System.out.println(s);
}
}
可能会遇到的问题
import java.io.UnsupportedEncodingException;
public class Encode {
public static void main(String[] args) throws UnsupportedEncodingException {
String src="这是我的abc";
byte[] bytes = src.getBytes();
String s;
//解码
//字符集不统一
s=new String(bytes,0,bytes.length,"gbk");
System.out.println(s);
//字节数不够
s=new String(bytes,0,bytes.length-1,"utf-8");
}
}