How to verify that what is stored in java byte is the complement, not the original code?

        We all know that the data in the computer is stored in the form of complement, so how to verify this statement?
        Compared with traditional numerical storage, Chinese character storage is more interesting. Let's take Chinese character storage as an example. With utf-8 encoding storage, a Chinese character occupies 3 bytes, and gbk encoding storage, a Chinese character occupies 2 bytes. In order to facilitate the calculation, we take the word "Zhang" as an example. The gbk decoding obtains the value. First , the gbk code of the word "Zhang" can be found through the online website as shown in the figure below: D5C5, converted to binary: 1101 0101 1100 0101. (If you won’t be converted, search for hexadecimal to binary by yourself!)
Hexadecimal code of "Zhang"
        Use the java code at the end of the article to verify that the result 1101 0101 1100 0101 is correct and get the result: (The code will be given at the end of the article)

[-43, -59]
[11010101, 11000101]

The binary is indeed correct. Is that the original code or the complement?
        If it is the original code 11010101 = -(64+16+4+1)= -85, 11000101 = -(64+4+1)=-69, which conflicts with the above -43 and -59. If it is a complement, take the first byte 11010101 as an example, the corresponding one's complement is 11010100, and the original code is: 10101011, which is equal to -(32+8+2+1)=-43, which is consistent with the above interface. The computer really stores data in the form of two's complement. The code is given below:


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;

public class ByteTest2 {
    public static void main(String[] args) {
        String s = "张";
        System.out.println(getByteArray(s, "gbk"));
    }

    public static ArrayList<String> getByteArray(String content, String charsetName) {
        byte[] bytes = new byte[0];
        try {
            bytes = content.getBytes(charsetName);
            System.out.println(Arrays.toString(bytes));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < bytes.length; i++) {
            strings.add(byteToBinaryString(bytes[i]));
        }
        return strings;
    }


    /**
     * byte转8位二进制字符串
     *
     * @param b
     * @return
     */
    public static String byteToBinaryString(byte b) {
        /*
         *  & 字符与运算
         *  例如: 0101 & 0110 = 0100
         *  0xFF = 255 => ‭1111 1111
         *  用byte和0xff相与,是因为int是32位,与0xff相与后就舍弃前面的24位,只保留后8位
         *  b & 0xFF 转换为int类型
         *
         *  0X100 = 256 => ‭0001 0000 0000
         *  bb + 0X100 补齐8位二进制‬
         */
        int bb = b & 0xFF;
        int bbb = bb + 0X100;
        String result = Integer.toBinaryString(bbb).substring(1);
        return result;
    }
}

Guess you like

Origin blog.csdn.net/zhangjin1120/article/details/107303114