Home  >  Article  >  Conversion between various bases

Conversion between various bases

尚
Original
2019-06-11 18:26:07103796browse

Conversion between various bases

1. Conversion between binary and decimal

1. Binary to decimal (regardless of integers and decimals, counting from the last digit , how many powers of 2 are multiplied by the number in each digit. This number is determined by the position of the number, starting from zero, and then adding)

Example: 01101011.001 to decimal

1乘2的-3次方=0.125
0乘2的-2次方=0
0乘2的-1次方=0
1乘2的0次方=1
1乘2的1次方=2
0乘2的2次方=0
1乘2的3次方=8 
0乘2的4次方=0
1乘2的5次方=32
1乘2的6次方=64
0乘2的7次方=0

Then: 1 2+0+8+0+32+64+0=107.125
01101011=107

2. Convert decimal to binary

Integer: The remainder method of division by 2 is a process of continuous division by 2 until the quotient appears 0 time position, the remainder is arranged in reverse order;
Example: Convert integer 23 to binary:

23除2商11余1
11除2商5余1
5除2商2余1
2除2商1余0
1除2商0余1

Then reverse the remainder: 23=10111

Decimal: Multiply 2 rounding method, that is, multiply the decimal part by 2, then take the integer part, continue to multiply the remaining decimal part by 2, then take the integer part, and multiply the remaining decimal part by 2 until the decimal part is zero. . If it can never be zero, it is the same as the rounding of decimal numbers. When retaining as many decimal places as required, the number will be rounded based on whether the next digit is 0 or 1. If it is zero, round it off. If it is 1, add one digit. In other words, 0 is rounded to 1. The reading should be from the previous integer to the following integer.

Example: Convert 0.125 to binary

Multiply 0.125 by 2 to get 0.25, then the integer part is 0, and the decimal part is 0.25;
Multiply 0.25 by 2, get 0.5, then the integer part is 0, and the decimal part is 0.5;
0.5 is multiplied by 2 to get 1.0, then the integer part is 1 and the decimal part is 0.0;
Read from the first digit to the last digit, which is 0.001.

23.125 Convert to binary 10111.001

Conversion between binary and octal (based on the conversion between binary and decimal)

Take three The unification method is to use the binary decimal point as the dividing point, take every three digits to the left (right) into one digit, and then add these three binary digits according to the weight. The resulting number is an eight-digit binary number. Then, Arrange in order, the position of the decimal point remains unchanged, and the number obtained is the octal number we are looking for. If you take three digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the three digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up three digits) The highest digit and lowest digit here are the same as in decimal, the first one is the highest digit, and the last one is the lowest digit.

Three-digit binary represents one octal. Because the largest decimal number of three-digit binary number (111) is 7, it is guaranteed that each digit is a number between 0-7

1. Convert binary to octal

Example: Convert 1100100 to octal

1100100 is split into: 001 100 100

0*2^2+0*2^1+1*2^0=1
1*2^2+0*2^1+0*2^0=4
1*2^2+0*2^1+0*2^0=4


Read it in sequence: 144
1100100=144

2. Convert octal number to binary

The corresponding relationship between octal number and binary number is as follows:

0=000 
1=001 
2=010 
3=011 
4=100 
5=101 
6=110 
7=111

Example: Convert octal number 653524 to binary

110 101 011 101 010 100

3. Conversion between binary and hexadecimal (the basis is still the conversion between binary and decimal)

Four-digit binary represents one hexadecimal digit. Because the largest four-digit binary number (1111) is the decimal representation 15, which is the hexadecimal representation F, so it is guaranteed that each digit is 0-F. number between.

1. Binary to hexadecimal

Example: 1100100 split 0110 0100
0110=6
0100=4
1100100=64

2. Convert hexadecimal to binary

The correspondence between hexadecimal and binary numbers:

1-0001
2-0010
3-0011
4-0100
5-0101
6-0110
7-0111
8-1000
9-1001
A-1010
B-1011
C-1100
D-1101
E-1110
F-1111

4. The relationship between decimal and hexadecimal Conversion

The algorithm is the same as the algorithm between binary and decimal, except that it changes from 2 to 16

1. Convert decimal to hexadecimal
Example: decimal number Convert 123 to hexadecimal
123 divided by 16 quotient 7 remainder B

7 divided by 16 quotient 0 remainder 7
The result is 7B
2. Hexadecimal conversion to decimal
Example : Hexadecimal number 2AF5

第0位:5 * 16^0=5
第1位:F * 16^1=240
第2位:A * 16^2=2560
第3位:2 * 16^3=81925*16^0+F*16^1+A*16^2+2*16^3=10997


2AF5=10997

Related recommendations: Decimal to binary formula

The above is the detailed content of Conversion between various bases. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn