本帖最后由 新手ing 于 2015-7-13 11:57 编辑
public class EncodeDemo {
public static void main(String[] args) throws IOException {
String s = "中文12";
byte[] bytes1 = s.getBytes();
for (byte b : bytes1) {
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] bytes2 = s.getBytes("gbk");
//一个中文占两个字节,英文一个字节
for (byte b : bytes2) {
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] bytes3 = s.getBytes("utf-8");
//一个中文占用三个字节,英文占用一个字节
for (byte b : bytes3) {
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] bytes4 = s.getBytes("utf-16be");
//中文英文都占连个字节
for (byte b : bytes4) {
System.out.print(Integer.toHexString(b&0xff)+" "); } }
}
输出结果
d6 d0 ce c4 31 32
d6 d0 ce c4 31 32
e4 b8 ad e6 96 87 31 32
4e 2d 65 87 0 31 0 32
项目默认采用gbk编码,中文占用两个字节,英文占用一个字节;
采用utf-8编码,中文占用三个字节,英文占用一个字节;
采用utf-16be编码,中文和英文各占两个字节
|
|