中文对应的字节
public class ByteBitDemo {
public static void main(String[] args) throws Exception{
String a = "尚";
byte[] bytes = a.getBytes();
for (byte b : bytes) {
System.out.print(b + " ");
String s = Integer.toBinaryString(b);
System.out.println(s);
}
}
}
运行程序:我们发现一个中文是有 3 个字节组成
我们修改 编码格式 , 编码格式改成 GBK ,我们在运行发现变成了 2 个字节
public static void main(String[] args) throws Exception{
String a = "尚";
// 在中文情况下,不同的编码格式,对应不同的字节
//GBK :编码格式占2个字节
// UTF-8:编码格式占3个字节
byte[] bytes = a.getBytes("GBK");
// byte[] bytes = a.getBytes("UTF-8");
for (byte b : bytes) {
System.out.print(b + " ");
String s = Integer.toBinaryString(b);
System.out.println(s);
}
}
运行程序
英文对应的字节
我们在看看英文,在不同的编码格式占用多少字节
public class ByteBit {
public static void main(String[] args) throws Exception{
String a = "A";
byte[] bytes = a.getBytes();
// 在中文情况下,不同的编码格式,对应不同的字节
// byte[] bytes = a.getBytes("GBK");
for (byte b : bytes) {
System.out.print(b + " ");
String s = Integer.toBinaryString(b);
System.out.println(s);
}
}
}
运行程序