1. Into the system
For integers, there are four base representations
(1) binary: 0,1, full 2 into 1
(2) Decimal: 0-9, full 10 into 1
(3) Octal: 0-7, 8 digits 1, starting with the number 0, such as 017
(4) Hexadecimal: 0-9 and a-F, the full hexadecimal 1, starting with 0x or 0x, a-F is case insensitive. Such as 0 x2f5a
2. Base conversion
2.1 Convert other bases to decimal
General rule: Starting with the lowest (on the right), extract the number in each bit, multiply it by the number of bits (number -1), and sum.
2.1.1 Converting binary to decimal
Rule: Starting with the lowest (right), extract the number in each bit, multiply it by 2 (digit -1), and sum.
Example: 1011 to decimal: 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0 = 11
2.1.2 Converting octal to decimal
Rule: Start with the lowest (on the right), extract the number in each bit, multiply it by 8 (bits -1), and sum.
Example: 01011 to decimal: 1*8^3 + 0*8^2 + 1*8^1 + 1*8^0 = 521
2.1.3 Converting hexadecimal to decimal
Rule: Starting with the lowest (right), extract the number in each bit, multiply it by 16 (digit -1), and sum.
Example: 0x1011 to decimal: 1*16^3 + 0*16^2 + 1*16^1 + 1*16^0 = 4113
2.2 Converting from decimal to other bases
General rule: Divide this number continuously by the base number to be converted until the quotient is 0, and then reverse the remainder of each step to the corresponding base.
2.2.1 Converting from decimal to binary
Rule: Divide the number by 2 until the quotient is 0, and then reverse the remainder of each step to the corresponding binary. Example: 56 to binary
56 ÷ 2 = 23 ÷ 2 = 7 ÷ 2 = 3 ÷ 2 = 3 ÷ 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 ÷ 2 = 1 111,000, so 56 to binary is 111,000Copy the code
2.2.2 Converting decimal to octal
Rule: Divide the number by 8 until the quotient is 0, then reverse the remainder of each step, which is the corresponding octal number. Example: 56 to octal
56 ÷ 8 = 7, remainder 07 ÷ 8 = 0, remainder 7 = 0, remainder 7 = 0, remainder 7 = 0, remainder 7 = 0, remainder 7 = 0, remainder 70Copy the code
2.2.3 Converting from decimal to hexadecimal
Rule: Divide the number by 16 until the quotient is 0, then reverse the remainder of each step to the corresponding hexadecimal. Example: 56 to hexadecimal
56 ÷ 16 = 3 remainder 8 3 ÷ 16 = 0 remainder 3 Then the remainder is reversed: 70 (hexadecimal is marked with 0x or 0x, this is fixed, so the hexadecimal is 0x38 or 0x38), that is, 56 to hexadecimal is 0x38 or 0x38Copy the code
2.3 Binary to octal or hexadecimal
2.3.1 Binary to octal
Rule: Convert binary numbers into octal numbers in groups of three digits (starting from the lowest digit). Example: 11010101 converted to octal
Binary 11 to octal is 3, binary 010 to octal is 2, and binary 101 to octal is 5, so 11010101 to octal is 0325Copy the code
2.3.2 Conversion from binary to hexadecimal
Rule: Convert binary numbers into hexadecimal numbers in groups of four digits (starting from the lowest digit). Example: 11010101 converted to hexadecimal
Group 11010101 to hexadecimal: binary 1101 to hexadecimal is D, binary 0101 to octal is 5, so 11010101 to hexadecimal is 0xD5Copy the code
Why do you choose three digits for binary to octal and four digits for binary to hexadecimal?
Because three digits in binary can be identified by exactly one octal digit, the range of numbers represented by three digits in binary is 0-7, and the range of numbers represented by exactly one octal digit is 0-7
Similarly, the 4-digit binary number is in the range 0-15, and the 8-digit number 0-f is in the range 0-15.
2.4 Convert octal or hexadecimal to binary
2.4.1 Conversion from octal to binary
Rule: Convert each digit of octal into a corresponding digit of binary. Example: 0237 to binary:
2 -> 010 3 -> 011 7 -> 111 so 0237 converted to binary is 10011111Copy the code
2.4.2 Converting hexadecimal to binary
Rule: Convert each hexadecimal bit into a corresponding four-digit binary. Example: 0x237 to binary:
2 -> 0010 3 -> 0011 7 -> 0111 then 0x237 converted to binary is 1000110111Copy the code