中文和英文对应的字节

中文对应的字节

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);
        }
    }
}

运行程序

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值