package 练习;
import java.io.UnsupportedEncodingException;
public class 编码 {
public static void main(String []args) throws UnsupportedEncodingException{
String string = "你好ABC";
byte[] byte1 = string.getBytes("utf-8");
//utf-8编码采用的汉字占三个字节,字母一个字节
for (byte b : byte1) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
byte[] byte2 = string.getBytes("utf-16be");
//java平台采用的双字节编码,全是占两个字节
for (byte b : byte2) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
byte[] byte3 = string.getBytes("gbk");
//gbk编码中汉字占两个字节
for (byte b : byte3) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
//二进制转换成字符串要采用相同的编码,否则会出现乱码
System.out.println("\n" + new String(byte1,"gbk"));
System.out.println(new String(byte1,"utf-8"));
}
}
字符编码
最新推荐文章于 2022-10-10 09:32:50 发布