[Java basic three] Java basic hexadecimal and mutual conversion

Computer languages ​​are essentially stored as binary numbers of 1 and 0, but octal, decimal, and hexadecimal data are commonly used. Since everyone has the most contact with and knows the decimal system in life, this article will not repeat the decimal system.

Binary

1. First sight of binary

  • Binary uses 0 and 1 as the code, and enters 1 every 2; for example, 3 (decimal)=11 (binary)=1*2+1.
  • In the computer, other hexadecimal algorithms are basically based on the binary system, because the computer only recognizes 0 and 1. For example, a hexadecimal number means that a hexadecimal code is represented by a 4-digit binary code number.
  • In the Java language, byte represents the smallest unit of measurement, and byte is composed of 8-digit binary numbers.

2. Binary and encoding

  • Generally, for English characters, one byte represents one character, but for Chinese characters, because the low-bit encoding has been used (early computers did not support Chinese, so in order to expand the support, the only way is to use more bytes The number) had to be extended to the higher digits.
  • The range of general character set encoding is utf-8>gbk>iso-8859-1(latin1)>ascll . The ascll code is the English abbreviation of the American Standard Information Interchange Code. It contains commonly used characters such as Arabic numerals, English letters and some printing symbols. Please pay attention to the difference between characters and numbers. For example, the decimal number corresponding to the character '0' is 48.
  • Unicode encoding contains many formats, utf-8 is the most commonly used one, and the name of utf-8 comes from the encoding that uses 8 bits per byte to represent a character. For a Chinese character, it requires 3 bytes to represent a Chinese character, but the people in Greater China expressed dissatisfaction and developed a set of gbk encoding format that uses two bytes to represent a Chinese character.

3. Binary related calculations

(1), binary bitwise AND operation &

 // 与运算: 0与任何数字运算为0,只有两个1与运算才为1

 1 & 1 = 1 

 0 & 1 = 0 

 51 & 5  即 0011  0011 & 0000  0101 =0000 0001 = 1

(2), bitwise OR operation|

// 或运算: 1与任何数字进行或运算都为1,只有两个0进行或运算才为0

1 | 0 = 1

1 | 1 = 1

0 | 0 = 0  

51 | 5 即 0011  0011 | 0000  0101 =0011  0111 =55

(3), XOR operation^

// 异或运算: 两个位的值不同,则结果为1,相同结果为0

1 ^ 1 = 0

1 ^ 0 = 1   

0 ^ 0 = 0

51 ^ 5 即 0011  0011 ^0000  0101 =0011  0110=54

(4) << left shift operator

Shift all binary bits of an operand to the left by several bits (the binary on the left is discarded, and the right is filled with 0)
 

11 << 2 = 44

-14 <<2 =-56

-14 binary (11111111 11111111 11111111 11110010) left shifted by 2 bits

            For 11111111 11111111 11111111 110010 00

        The result is (-56)

How negative numbers are represented in binary ( negative numbers are represented by the complement of positive numbers )

Original code: an integer is converted into a binary number according to the absolute value

Inverse code: Invert the binary number bit by bit

Complement: One's complement plus 1 

Take -14 for example

原码 14             即 00000000  00000000  00000000  00001110

反码                   11111111  11111111  11111111  11110001

补码                   11111111 11111111  11111111  11110010

所以-14  的二进制是     11111111 11111111  11111111  11110010

 Suppose we get binary. Let us find integers by going backwards to get the opposite.

二进制是                11111111  11111111  11111111  11110010

得到反码减1             11111111  11111111  11111111   11110001

原码                   00000000  00000000  00000000   00001110


即  1110  = 14  所以取反  就是-14

(5) >> a right shift operator

Shift all binary digits of an operand to the right by a few bits, complement 0 to the left for positive numbers, and 1 to the left for negative numbers.

4 >> 2 = 1;

-14 >> 2 = -4;

 

Octal

Octal, Octal, abbreviated OCT or O, is a counting method based on 8 that uses 0, 1, 2, 3, 4, 5, 6, 7 and enters 1 every eight.

When the Java language declares a number as octal, it needs to add 0 in front of the number, that is (0123 represents octal, but 123 indicates decimal)

int   a   =   100;  // 表示为十进制100

int   a   =   0144; // 表示八进制100

Do remember that when using octal expression, you can not miss the first 0. Otherwise, the computer will treat it as decimal. However, there is one place where adding 0 cannot be used when using octal numbers. That is the "escape character" expression that we learned earlier to express characters.

Since the octal system is not used much in future development, I will only give you a brief introduction here, and interested readers can learn by themselves.

 

Hexadecimal

Hexadecimal (abbreviated as hex or subscript 16) is a carry system that enters 1 every hexadecimal in mathematics . It is generally represented by numbers 0 to 9 and letters A to F (or a~f), where: A~F means 10~15, these are called hexadecimal numbers .

Note: Hexadecimal is entered every hexadecimal. Generally, the computer's 1-pair hexadecimal number will also be mixed with decimal.

The hexadecimal number must start with 0x. For example, 0x1 represents a hexadecimal number. And 1 represents a decimal. In addition, such as: 0xff, 0xFF, 0X102A, and so on. The x is not case sensitive. (Note: The 0 in 0x is the number 0, not the letter O)

 int   a   =   0x100F;  

 int   b   =   0x70   +   a;  

 

Conversion between bases (Java API)

 1. Decimal conversion to other hexadecimal algorithms

int number = 25;
		
//十进制转成十六进制:
String a = Integer.toHexString(number);
System.out.println(a);    //19

//十进制转成八进制
String b = Integer.toOctalString(number);
System.out.println(b);    //31

//十进制转成二进制
String c = Integer.toBinaryString(number);
System.out.println(c);    //11001

2. Conversion between other bases

String binary = "101011";

//二进制转十进制
Integer.valueOf(binary, 2).toString(); // 43
//二进制转八进制
Integer.toOctalString(Integer.parseInt(binary, 2)); // 53
//二进制转十六进制
Integer.toHexString(Integer.parseInt(binary, 2)); // 2b


String octal = "520";

//八进制转成十进制
Integer.valueOf(octal, 8).toString(); // 336
//八进制转成二进制
Integer.toBinaryString(Integer.valueOf(octal, 8)); // 101010000
//八进制转成十六进制
Integer.toHexString(Integer.valueOf(octal, 8)); // 150

String hex = "FA";

//十六进制转成十进制
Integer.valueOf(hex, 16).toString();
//十六进制转成二进制
Integer.toBinaryString(Integer.valueOf(hex, 16));
//十六进制转成八进制
Integer.toOctalString(Integer.valueOf(hex, 16));

Finally, the hexadecimal problem will be implemented in the context of the entire computer, this article is just a concise and convenient to help Java learning in the future! !

 

Previous post:   [Java Foundation II] JDK installation and environment variable configuration                        

Next:

 

Guess you like

Origin blog.csdn.net/dgxin_605/article/details/85010628