Java SE (12) extension-base conversion

Number system: counting according to the principle of carry is called the carry technology system.

          Different number systems have different bases and positions.

table of Contents

Base

Position power

Writing rules

Written rules

Programming rules

Base conversion

Low to high

High to low


Base

Concept: The number of digits in each number system is called the base of the number system.

Number system Base Digital
Binary 2 0 1
Octal 8 0 1 2 3 4 5 6 7
Decimal 10 0 1 2 3 4 5 6 7 8 9
Hexadecimal 16 0 1 2 3 4 5 6 7 8 9 A B C D E F

 

  Every 2 into 1

  Every 8 enters 1

  Every 10 enters 1

  Every 16 enters 1

 


Position power

Concept: In each number system, the position of a number is different, and the value represented by it is different, which is called different position weight.

           For example: In binary 1111.11, the leftmost 1 represents 1*2^3=16, and the rightmost 1 represents 1*2^-2=0.25.

                     Octal 2222.22 2 2*8^3 2*8^-2

                    Decimal 3333.33 3 3*10^3 3*10^-2

                Hexadecimal 4444.44 4 4*16^3 4*16^-2


Writing rules

Written rules

Method 1: Add English letters after the number

          B (binary) represents a binary number , and binary 101 can be written as 101B .

          O (octonary) represents an octal number , and octal 101 can be written as 101O or 101Q .

          D (decimal) represents a decimal number , and decimal 101 can be written as 101D .

          H (hexadecimal) represents a hexadecimal number , and hexadecimal 101 can be written as 101H .

Method 2: Add a number subscript outside the brackets

         

Programming rules

      Binary integer, requires 0b or 0B beginning, such as: 0b11

     Decimal integer, such as: 99, -500, 0

     An octal integer, which must start with 0 , such as: 015

    Hexadecimal number, requires 0x or 0X to start, such as: 0x15


Base conversion

Low to high

Summation

1. Binary -> Decimal

For example: 1 0 1 1 0 0 1. 1 0 1 1 1 B

         =1*2^6+0+1*2^4+1*2^3+0+0+1*2^0+1*2^-1+0+1*2^-3+1*2^-4+1*2^-5

         =   64  +0+   16  +    8   +0+0+    1    +  0.5   +0+ 0.125+0.0625+0.03125

         = 89.71875D

2. Binary -> octal (take the three -in-one -> expand their sums right position)

With the decimal point as the boundary, divide it into three groups of one digit to the left and right , and fill in the zeros if the three digits are not enough. After grouping, the corresponding octal numbers are formed.

For example: 1 011 001. 101 11 B

         =001 011 001 . 101 110 B

         =  1      3    1   .   5      6  Q

3. Binary -> Hex (take the four -in-one -> expand their sums right position)

With the decimal point as the boundary, divide it into four groups of one digit to the left and right , and fill in the zeros if the four digits are not enough. After the group is divided, it corresponds to a hexadecimal number.

For example: 101 1001. 1011 1 B

          = 0101 1001 . 1011 1000  B

          =    5       9   .     B       8    H

4. Octal -> Decimal

    For example: 1 6 Q

            =1*8^1+6*8^0 

            =    8    +   6 

            =14D

5. Octal -> Hexadecimal

   Method: Octal -> Decimal -> Hexadecimal

High to low

Divide by two backward remainder method

1. Decimal -> Binary

The integer part is divided by 2 and the remainder is reversed , and the decimal part is multiplied by 2 and is rounded forward .

For example: 89.71875D=1011001.10111B

         

2. Octal -> Binary

Method 1: Each bit of the octal system is divided by 2 to take the remainder , each corresponding to three binary digits, and zeros are added to the leftmost when insufficient .

              For example: 276.15Q=10 111 110.001 101B

              

Method 2: Expand each octal number into three binary digits ( take one into three ).

              For example: 2 7 6. 1 5 Q

                      =010 111 110 . 001 101 B

3. Hexadecimal -> Binary

Method 1: Divide each bit of hexadecimal by 2 and take the remainder . Each corresponds to four binary digits. When insufficient, zero is added to the left .

              For example: 3AC.1EH=11 1010 1100.0001 1110 B

              

Method 2: Expand each hexadecimal number into four binary digits ( take one into four ).

              For example: 3 A C. 1 E H

                    =0011 1010 1100 . 0001 1110 B

4. Decimal -> Octal

Divide the     integer part by 8 and take the remainder .

    For example: 14D=16Q

    

 

Guess you like

Origin blog.csdn.net/wqh101121/article/details/111453377