JAVA - Binary, Decimal, Octal, Hexadecimal Conversion

Binary groups of three are octal, and groups of four are hexadecimal

1. Binary and decimal conversion

(1) Binary to decimal

Method: "Expand Sum by Weight"

【Example】:Integer conversion

[Example]: decimal conversion (0.101)2 = 1x2-1 +0x2-2 +1x2-3 = (0.625)10

Rule: The number of times in the one's place is 0, the number of times in the ten's place is 1, ..., increasing in turn, and the ten

The number of digits in the quantile is -1, the number of digits in the percentile is -2, ... , in descending order.

(2) Decimal to binary

Convert decimal integers to binary numbers: "divide by 2 and take the remainder, and arrange in reverse order" (divide by two and take the remainder)

【example】:

89÷2 ……1

44÷2 ……0

22÷2 ……0

11÷2 ……1

5÷2 ……1

2÷2 ……0

1

(89)10=(1011001)2
Decimal decimal to binary number: "Multiply by 2 to round up and arrange in order" (multiply by 2 to round up)

Note: Not every decimal fraction can be converted to a binary number with a limited number of digits.

[Example]: (0.625) 10 = (0.101) 2

0.625X2=1.25……1

0.25 X2=0.50……0

0.50 X2=1.00 ……1

2. Binary and Octal Conversion

(1) binary to octal

Starting from the decimal point, the integer part goes to the left and the fractional part goes to the right. Every 3 digits is a group of digits represented by an octal number. If there are less than 3 digits, use "0" to make up 3 digits to get an octal number.

[Example]: Convert binary 10110.0011 to octal:

0 1 0 1 1 0 . 0 0 1 1 0 0

2 6 . 1 4

That is: (10110.0011)2 = (26.14)8

(2) Octal to binary

Convert each octal number to a 3-bit binary number, and you get a binary number.

【Example】: Convert octal 37.416 to binary number:

3 7 . 4 16

011 111 .100001 110

That is: (37.416)8 = (11111.10000111)2

3. Binary and hexadecimal conversion

The correspondence between hexadecimal numbers and binary numbers is as follows:

0000 -> 0 0100 -> 4 1000 -> 8 1100 -> C

0001 -> 1 0101 -> 5 1001 -> 9 1101 -> D

0010 -> 2 0110 -> 6 1010 -> A 1110 -> E

0011 -> 3 0111 -> 7 1011 -> B 1111 -> F

(1) binary to hexadecimal

When converting a binary number into a hexadecimal number, just start from the decimal point, divide it into a group of four binary digits to the left or right (the less than four digits can be filled with 0), and then write out the corresponding binary number of each group. Just a hexadecimal number.

[Example]: Convert the binary number 1100001.111 to hexadecimal:

0110 0001 .1110

6 1 . E

That is: (1100001.111) 2 = (61.E) 16

(2) Hexadecimal to binary

Convert each hexadecimal number to a 4-bit binary number, and you get a binary number.

[Example]: Convert the hexadecimal number 5DF.9 to binary:

5 D F . 9

0101 1101 1111 .1001

That is: (5DF.9)16 = (10111011111.1001)2

4. Octal to Decimal Conversion

(1) Octal to decimal

The overall order and the decimal point remain unchanged, the one digit multiplied by 8 to the 0th power + the tens digit multiplied by the 1 power of 8...= decimal number, that's all. The fractional part starts from left to right multiplied by the negative power of 8, and so on.
insert image description here

(2) Decimal to octal

​ The overall order and decimal point remain unchanged, the integer part is divided by 8, the remainder is arranged in reverse from left to right, the decimal part is multiplied by 8, and the integer is arranged from left to right.

​ Integer part:
insert image description here

decimal part:
insert image description here

5. Decimal and hexadecimal conversion

(1) Hexadecimal to decimal

The weight of the 0th digit of the hexadecimal number is 16 to the power of 0, the weight of the first digit is 16 to the power of 1, and the weight of the second digit is 16 to the power of 2...

[Example]: Example: 2AF5 converted to decimal: 5 * 16^0 + F * 16^1 + A* 16^2 + 2 * 16^3 = 10997

(2) Decimal to hexadecimal
insert image description here

Divide by 16 and take the remainder, until the quotient is 0, insert a picture description
here 255Fid% 2522% 253A% 2522163392687216780262552110% 2522% 252C% 2522scm% 2522% 253A% 252,220,140,713.130102334 ...% 2522% 257D & the request_id = 163392687216780262552110 & biz_id = 0 & utm_medium = distribute.pc_search_result.none-Task-Blog-2 All sobaiduend ~ default-1-86035910.pc_search_result_control_group & utm_term =%E5%8D%81%E8%BF%9B%E5%88%B6%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%8D%81%E5%85%AD% E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2&spm=1018.2226.3001.4187.

Guess you like

Origin blog.csdn.net/qq_43398758/article/details/120700738