Byte和bit

Byte和bit

1.概述
在这里插入图片描述
2.获取字符串byte

package com.atguigu.bytebit;

/**
 * ByteBit
 *
 * @Author: 尚硅谷
 * @CreateTime: 2020-03-17
 * @Description:
 */
public class ByteBit {
    public static void main(String[] args) {
        String a = "a";
        byte[] bytes = a.getBytes();
        for (byte b : bytes) {
            int c=b;
            // 打印发现byte实际上就是ascii码
            System.out.println(c);
        }
    }
}

运行程序
在这里插入图片描述
3.byte对应bit

package com.atguigu.bytebit;

/**
 * ByteBit
 *
 * @Author: 尚硅谷
 * @CreateTime: 2020-03-17
 * @Description:
 */
public class ByteBit {
    public static void main(String[] args) {
        String a = "a";
        byte[] bytes = a.getBytes();
        for (byte b : bytes) {
            int c=b;
            // 打印发现byte实际上就是ascii码
            System.out.println(c);
            // 我们在来看看每个byte对应的bit,byte获取对应的bit
            String s = Integer.toBinaryString(c);
            System.out.println(s);
        }
    }
}

在这里插入图片描述
4.中文对应的字节

// 中文在GBK编码下, 占据2个字节
// 中文在UTF-8编码下, 占据3个字节
package com.atguigu;

/**
 * ByteBitDemo
 *
 * @Author: 尚硅谷
 * @CreateTime: 2020-03-16
 * @Description:
 */
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);
        }
    }
    
    
}

在这里插入图片描述
我们修改编码格式 , 编码格式改成 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);
        }
    }

在这里插入图片描述
5.英文对应的字节
我们在看看英文,在不同的编码格式占用多少字节

package com.atguigu.bytebit;

/**
 * ByteBit
 *
 * @Author: 尚硅谷
 * @CreateTime: 2020-04-12
 * @Description:
 */
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);
        }
    }
}

在这里插入图片描述

package com.atguigu.bytebit;

/**
 * ByteBit
 *
 * @Author: 马伟奇
 * @CreateTime: 2020-05-05
 * @Description:
 */
public class ByteBit {
    public static void main(String[] args) {
        String a = "a";
        byte[] bytes = a.getBytes();
        for (byte aByte : bytes) {
            int c = aByte;
            System.out.println(c);
            // byte 字节,对应的bit是多少
            String s = Integer.toBinaryString(c);
            System.out.println(s);
        }
    }
}
package com.atguigu.bytebit;

/**
 * ByteBitDemo
 *
 * @Author: 马伟奇
 * @CreateTime: 2020-05-05
 * @Description:
 */
public class ByteBitDemo {
    /**
     * 根据编码的格式不一样,对应的字节也不一样
     * 如果是UTF-8:一个中文对应的是三个字节
     * 如果是GBK : 一个中文对应的是二个字节
     *
     * 如果是英文,就无所谓编码格式
     */
    public static void main(String[] args) throws Exception{
        String a = "A";
        byte[] bytes = a.getBytes();
        for (byte aByte : bytes) {
            System.out.println(aByte);
            String s = Integer.toBinaryString(aByte);
            System.out.println(s);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值