天天看点

密码学之Byte和bit

前言

Byte : 字节. 数据存储的基本单位,比如移动硬盘1T , 单位是byte

bit : 比特, 又叫位. 一个位要么是0要么是1. 数据传输的单位 , 比如家里的宽带100MB,下载速度并没有达到100MB,一般都是12-13MB,那么是因为需要使用 100 / 8

关系: 1Byte = 8bit

一、获取字符串byte

package com.atguigu.bytebit;
/**
 * @author JsonHao😋
 * @date 2020年9月10日 下午10:58:59
 */
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

package com.atguigu.bytebit;

/**
 * @author JsonHao😋
 * @date 2020年9月10日 下午10:58:59
 */
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);
        }
    }
}

      

三、 中文对应的字节

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

/**
 * @author JsonHao😋
 * @date 2020年9月10日 下午11:09:16
 */
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);
  }
    }      

四、 英文对应的字节

我们在看看英文,在不同的编码格式占用多少字节

package com.atguigu.bytebit;

/**
 * @author JsonHao😋
 * @date 2020年9月10日 下午10:58:59
 */
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);
        }
    }
}

      

运行程序

密码学之Byte和bit