Computer Basics--Conversion Between Bases

foreword

When learning computers, it is inevitable to need to understand the concept of bases and how to convert between them.

1. Introduction to base

1.1 Concept introduction

The concept of binary:
Binary is a number system widely used in computing technology. Binary data is a number represented by two digits of 0 and 1. Its base is 2, the carry rule is "every two into one", the borrow rule is "borrow one as two", the binary in the computer is a very small switch, 1 is used to represent "on", and 0 is used to represent "close".
The concept of octal:
octal, Octal, abbreviated OCT or O, a counting method based on 8, using eight numbers of 0, 1, 2, 3, 4, 5, 6, 7, every octal enters 1. Some programming languages ​​often start with the number 0 to indicate that the number is octal. Octal numbers and binary numbers can correspond bit by bit (one bit in octal corresponds to three bits in binary), so it is often used in computer languages.
The concept of hexadecimal:
Hexadecimal (English name: Hexadecimal), is a representation method of data in the computer. It is different from the expression in our daily life. It consists of 0-9, AF, letters are not case sensitive. The corresponding relationship with base 10 is: 0-9 corresponds to 0-9; AF corresponds to 10-15; numbers in base N can be represented by numbers from 0 to (N-1), and those exceeding 9 are represented by letters AF.
The concept of decimal:
Decimal is actually a common base language all over the world, 1, 2, 3, 4, every 9 enters 1...


1.2 Representation in base

For base, there are four representations

1. Binary: 0,1, full 2 ​​into 1
2. Decimal: 0-9, full 10 into 1
3. Octal: 0-7, full 8 into 1, start with the number 0 to represent
4. Hexadecimal: 0 -9 and AF, full 16 into 1, starting with 0X or 0X

decimal hexadecimal Octal binary
0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
12 C 14 1100
13 D 15 1101
14 E 16 1110
15 F 17 1111
16 10 20 10000
17 11 21 10001

2. Hexadecimal conversion

The hexadecimal conversion is divided into four groups to introduce the conversion rules and demonstrations in turn.
The first group (other hexadecimal conversion)

Binary to Decimal
Octal to Decimal
Hexadecimal to Decimal

The second group (decimal to other bases)

Decimal to Binary
Decimal to Octal
Decimal to Hexadecimal

The third group (binary to other bases)

Binary to Octal
Binary to Hexadecimal

The fourth group (other bases to binary)

Octal to Binary
Hexadecimal to Binary


2.1 The first group (other base to decimal)

2.1.1 Convert binary to decimal

Rules: Starting from the lowest bit (rightmost), extract the number of each bit, multiply by 2 to the power of (number of digits - 1), and then add and sum.
Demo:

110010=0*2+1*2+0*2*2+0*2*2*2+1*2*2*2*2 +1*2*2*2*2*2 = 50

It may not be so easy to see at once, here is another example:

Binary 111, convert it to decimal
Starting from the right, each digit * 2 (number of digits - 1) power
111 = 1 * 2 to the 0th power ==> 2 to the 0th power is 1 + 1 * 2 1 power + 1 * 2 to the 2 power = 1+ 2+4 = 7
, so 111 converted to decimal is equal to the number 7

You can use a calculator to do the verification, click on the calculator to view and then click on the column of the programmer to go to this position.
insert image description here
After entering 111 on the binary page, and then clicking on the decimal system, the corresponding value will be displayed. You can see that the result is 7.
insert image description here

2.1.2 Octal to Decimal

Rules: Starting from the lowest bit (rightmost), extract the number on each bit, multiply by 8 to the power of (number of digits - 1), and then add and sum.
Demo:

0777  = 7 * 1 + 7 * 8 + 7 * 8 * 8 = 511

It's the same way, but this time it's multiplied by 8.only
insert image description here

2.1.3 Hex to Decimal

Rules: Starting from the lowest bit (rightmost), extract the number on each bit, multiply by 16 to the power of (number of digits - 1), and then add and sum.
Demo:

0X9CA1 = 1 * 1 + 10 * 16 + 12 * 16 * 16  + 9 * 16 * 16 * 16 = 40097

The front 0X is the fixed writing method of hexadecimal, and the content to be converted in hexadecimal is 9CA1.
After calculation, we can see that the number converted from hexadecimal to decimal is still very large.
insert image description here
insert image description here


2.2 The second group (decimal to other bases)

2.2.1 Convert decimal to binary

Rules: Divide the number by 2 continuously until the quotient is 0, and then reverse the remainder obtained at each step, which is the corresponding binary.
Demo:

For example, there is a decimal number 56. To convert it into binary, you need to divide the number by 2 until it cannot be divided into 2 integers; then reverse the remainder obtained in each step to get the binary value.
As shown in the figure below, after the calculation of the whole process, the value converted from decimal to binary is 111000.

insert image description here
insert image description here
insert image description here

2.2.2 Convert decimal to octal

Rules: Divide the number by 8 continuously until the quotient is 0, and then reverse the remainder obtained at each step, which is the corresponding octal.
Demo:

For example, a decimal number is 160. To convert it into octal, you need to divide this number by 8 until it cannot be divided into 8 integers; then reverse the remainder obtained in each step, and you will get the octal value.
As shown in the figure below, after the calculation of the whole process, the value converted from decimal to octal is 0240.
insert image description here

2.2.3 Decimal to Hexadecimal

Rule: keep dividing the number by 16 until the quotient is 0, and then reverse the remainder obtained at each step, which is the corresponding hexadecimal.
Demo:

For example, a decimal number is 350. To convert it into hexadecimal, you need to divide this number by 16 until it cannot be divided into 16 integers; then reverse the remainder obtained in each step, and you will get hexadecimal made value.
As shown in the figure below, after the calculation of the whole process, the value converted from decimal to hexadecimal is 0X15E.

Remember why 14 is E, because after the hexadecimal number exceeds 9, it is represented by the letter AF.
![Insert picture description here](https://img-blog.csdnimg.cn/aeef1c863c314bae912604e9fd719ed1.png


2.3 The third group (binary to other bases)

2.3.1 Convert binary to octal

Rules: Convert binary numbers into groups of three digits (combined from the low bit) into corresponding octal numbers.
Demo:

1010011

Divide the binary number into groups of three digits, starting from the right, if there are not enough three digits, you can add 0 in front; that is, 001 010 011 Do you
remember how binary is calculated, and
the result is 1 2 3 when every 2 enters 1.

2.3.2 Binary to hexadecimal

Rules: convert the binary number into a corresponding hexadecimal number in groups of four digits (combined from the low bit).

11101101

Divide the binary number into four groups, starting from the right, if there are not enough four digits, you can add 0 in front; that is,
the result of 1110 1101 is 0XED.

2.4 The fourth group (transfer from other bases to binary)

2.4.1 Octal to Binary

Rules: Convert each bit of octal into a corresponding 3-bit binary number, starting from right to left.
Demo:

0403

Convert each octal number into the corresponding three-digit binary.
3 corresponds to three-digit binary is 011,
0 corresponds to three-digit binary is 000,
and 4 corresponds to three-digit binary is 100.
The combination is 100000011

insert image description here
insert image description here

2.4.2 Convert hexadecimal to binary

Rules: Convert each bit of hexadecimal into a corresponding 4-bit binary number, starting from right to left.
Demo:

0X1AE

Convert each hexadecimal number into the corresponding four-digit binary
E corresponding to four-digit binary is 1110
A corresponding to four-digit binary is 1010
1 corresponding to four-digit binary is 0001
together is 000110101110, and the previous 0 can not be written that is 110101110

insert image description here
insert image description here

Summarize

There are so many conversions between bases. Learning bases can also deepen your understanding of computers. If you think the content is okay, you can like it and support it!
insert image description here

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129929106