1.gbk
gbk编码格式中一个中文占两个字节,英文占一个字节
String s = "你好ABC";
byte[] bytes = s.getBytes("gbk"); //gbk编码中文占两个字节,英文占一个字节 for (byte b : bytes) { //把字节转化成int以16进制显示(只填充了int的低八位),与0xff(1111 1111)相与只取低八位。 System.out.print(Integer.toHexString(b & 0xff)+" "); }执行结果:
c4 e3 ba c3 41 42 43
2.utf-8
byte[] bytes1 = s.getBytes("utf-8");
//utf-8中文占三个字节,英文占1个字节 for (byte b : bytes1) { System.out.print(Integer.toHexString(b & 0xff)+" "); } System.out.println();执行结果:
e4 bd a0 e5 a5 bd 41 42 43
3.utf-16be
//java是双字节编码utf-16be(编译形成class文件后),中文占两个字节,英文占两个字节
byte[] bytes2 = s.getBytes("utf-16be"); for (byte b : bytes2) { System.out.print(Integer.toHexString(b & 0xff)+" "); }执行结果:
4f 60 59 7d 0 41 0 42 0 43