Hexadecimal (base conversion) (Python)

Hex

1. Base conversion

  1. Let’s look at decimal and binary first

(1) The essence of base conversion

We use short division to convert decimal to binary.

“Number system” is just a system of symbols to represent the amount of the referenced “quantity”. We use the symbol “1” to represent this concept of “quantity”. The “quantities” in nature are infinite. It is impossible for us to create a symbol for each “quantity”. No one will remember such a system. Therefore, finite symbols must be arranged and combined according to certain rules to represent this infinite “quantity”. Symbols are limited, and the number of combinations of these symbols according to certain rules is infinite. Decimal is a permutation and combination of 10 symbols, and binary is a permutation and combination of 2 symbols.

There is a basic principle when performing base conversion: the amount of “amount” expressed cannot be changed after conversion. 111 apples in binary are the same number as 7 apples in decimal.

It can be seen that the difference between adjacent digits is one power of the base system.

The base system, in the final analysis, is the principle of place value, that is: the same number, placed in different digits, represents different sizes of “quantity”. For example: In the decimal system, 1 in the hundreds place represents 100, and 1 in the tens place represents 10.

In any base system, each number can be expanded according to bit weight into the form of multiplying the number on each digit by the bit weight of the corresponding digit, and then adding them together, such as:

Decimal 123=1×100 + 2×10 + 3×1

Decimal 9876=9×1000 + 8×100 + 7×10 + 6×1

Question: Why are the corresponding numbers 1000, 100, 10, and 1? Why not 4, 3, 2, 1?

Answer: In decimal system, when every ten is full, one is advanced, and when it is more than ten, one is advanced. Therefore, if you want to enter the third digit, you must have 10×10; the fourth digit must be 10×10×10.

So we know:

For decimal, from low to high, multiply by 10^0, 10^1, 10^2, 10^3…, that is, 1, 10, 100, 1000

For binary digits, from low to high, multiply by 2^0, 2^1, 2^2, 2^3…, that is, 1, 2, 4, 8,…

Next we start converting the base (taking decimal to binary as an example):

It turns out that our decimal digits are called thousands, hundreds, tens…

Now the binary digits have become eight digits, four digits, two digits…

Imitating the above decimal bitwise expansion method, expand the binary number 1011 according to weight: 1011=1×2^3 + 0×2^2 + 1×2^1 + 1×2^0=1×8 + 0× 4 + 1×2 + 1×1=8 + 2 + 1=11

Next we convert decimal to binary:

Relatively small numbers can be converted back directly by splitting

For example, 13, we arrange the digits in eight, four, and two digits, and we cannot write sixteen, because once the symbol on the “sixteen” digit is “1”, it means that there is one 16, even if the following digits are The symbols are all “0”. After expanding this binary number according to the weight bits, and calculating according to the decimal operation rules, the number obtained is also greater than 13. That can only contain the digit “eight” at most. 13-8=5, 4 out of 5, 5-4=1

Okay, we know that 13=1*8 + 1*4 + 0*2 + 1*1. Put the symbols “1”, “1”, “0” and “1” on the digits:

Eight, four, two, one

1 1 0 1

So the decimal number 13 = the binary number 1101

If you want to know the truth, please carefully taste the following recursion principle (it doesn’t matter if you don’t know recursion):

The end of a decimal number 321 is 1, which means it must be… + 1. The ellipsis part must be a multiple of 10, so the end of a decimal number 321 is 1, which means that the decimal number divided by base 10 must have a remainder of 1. Therefore, the remainder after the first division by 10 should be placed in the last digit of the decimal system, the “ones digit”, which means that the sign in the ones digit is 1.

By analogy, a binary number 111 (note that the value is not equal to the decimal value 111 above) ends with 1, which means it must be… + 1, and the preceding ellipses are all multiples of 2. Therefore, a binary number ends with 1, which means that its corresponding decimal number must have a remainder of 1 when divided by base 2. Therefore, the remainder after the first division by 2 should be placed in the last binary digit “one bit”, which means that the sign on one bit is 1.

If the “tens” of a decimal number 321 is 2, we want to convert it to the (1) case. Then we erase the end of the decimal number, that is, subtract 1 from the “ones digit”, and then divide it by base 10 to get 32. In this way, the original “2” on the “tens” place falls to the “ones” place. Then do the processing of (1) on 32.

By analogy, if the “two digits” of a binary number 111 is 1 and we want to convert it to (1), then we erase the end of the binary number, that is, subtract the 1 on the “one digit”, and then Divide by base 2, you get 11. In this way, the “1” originally on the “two digits” fell to the “one digit”. Then do the processing of (1) for 11.

Summary: In fact, this process is to find out the symbols on each digit.

Now you should be able to answer the following question: Why can short division achieve base conversion? Why do we need to divide by base every time? Why put the remainders in reverse order?

Converting the R system to decimal system is to expand according to the weight, put the expansion formula in the decimal system, and then calculate according to the “decimal system” operation rules. Because it is decimal, 2, 3, 4, 5, 6, 7, 8, and 9 are allowed. Therefore, 2 raised to the nth power does not need to be written as an exponent, but can be represented by eight other symbols.

(2) Decimal —> Binary

For the integer part, use the dividend to divide by 2 repeatedly. Except for the first time, every time you divide by 2, take the integer part of the previous quotient as the dividend and write down the remainder each time. In addition, the last remaining digit of the obtained quotient is the highest digit of the binary number being sought.

337fd8a31e3841f79302e33e57685375.png

Decimal to binary

For the decimal part, continuously multiply by base 2 and take out the integer part in turn until the decimal part of the result is 0. Therefore, this method is called “base multiplication and rounding method”.

Given a decimal number, such as 6, what if you convert it into a binary number?

Converting a decimal number into a binary number is a process of continuous division by 2:

Divide the number to be converted by 2 to get the quotient and remainder,

Continue dividing the quotient by 2 until the quotient is 0. Finally, arrange all the remainders in reverse order, and the number obtained is the conversion result.

Sound confused? Explain with examples. For example, you want to convert 6 to a binary number.

“Divide the number to be converted by 2 to get the quotient and remainder.”

1d864ac1bbc14c0994ac71d82207a262.png

Ten turns to two diagram

Then: the number to be converted is 6, 6 ÷ 2, the obtained quotient is 3, and the remainder is 0.

“Continue dividing the quotient by 2 until the quotient is 0…”

Now the quotient is 3, not 0 yet, so continue dividing by 2.

That is: 3 ÷ 2, the quotient is 1, and the remainder is 1.

“Continue dividing the quotient by 2 until the quotient is 0…”

Now the quotient is 1, not 0 yet, so continue dividing by 2.

Then: 1 ÷ 2, the quotient is 0 and the remainder is 1

“Continue dividing the quotient by 2 until the quotient is 0…Finally arrange all the remainders in reverse order”

Very good! Now the quotient is 0.

We calculated three times and got the remainders: 0, 1, 1. Arranging all the remainders in reverse order, that’s: 110!

6 converted to binary, the result is 110.

Change the above paragraph to use a table to express it as:

dividend

calculation process

business

remainder

6

6/2

3

0

3

3/2

1

1

1

1/2

0

1

(In computers, ÷ is represented by /)

(3) Binary —> Decimal

Convert binary number to decimal number

The weight of the 0th bit of a binary number is 2 raised to the 0th power, and the weight of the 1st bit is 2 raised to the 1st power…

Therefore, given a binary number: 0110 0100, converted to decimal:

Here is the vertical version:

0110 0100 converted to decimal

Convert from right to left

Bit 0 0 * 2^0 = 0

Bit 1 0 * 2^1 = 0

Position 2 1 * 2^2 = 4

Bit 3 0 * 2^3 = 0

Bit 4 0 * 2^4 = 0

Number 5 1 * 2^5 = 32

Bit 6 1 * 2^6 = 64

Bit 7 0 * 2^7 = 0

Formula: Nth digit 2(N)

————————–

100

Calculated in horizontal form:

0 * 2^0 + 0 * 2^1 + 1 * 2^2 + 0 * 2^3 + 0 * 2^4 + 1 * 2^5 + 1 * 2^6 + 0 * 2^7 = 100

All numbers other than 0 raised to the 0th power are 1, but any number multiplied by 0 is always 0, so we can also directly skip the bits with a value of 0:

1 * 2^2 + 1 * 2^5 + 1 * 2^6= 100

2. Let’s discuss the relationship between decimal and hexadecimal systems

(1) Decimal —> Hexadecimal

The method of converting a decimal number to hexadecimal is similar to the method of converting a decimal number to binary. The only change is that the divisor changes from 2 to 6.

Let’s look at an example of how to convert the decimal number 120 into a hexadecimal number.

Represented in a table:

dividend

calculation process

business

remainder

120

120/6

20

0

20

20/6

3

2

2

2/6

0

2

120 is converted to octal, and the result is: 320.

(2) Hexadecimal —> Decimal

In hexadecimal system, every 6 is entered into 1.

Hexadecimal numbers use six numbers from 0 to 5 to express a number.

The weight of the 0th digit of the hexadecimal number is 6 raised to the power 0, the weight of the first digit is 6 raised to the power 1, and the weight of the second digit is 6 raised to the power 2…

Therefore, given an octal number: 1505, converted to decimal: 401

Expressed vertically:

1507 converted to decimal.

Bit 0 5* 6^0 = 5

Bit 1 0 * 6^1 = 0

Number 2 5 * 6^2 = 180

Position 3 1 * 6^3 = 216

2. Algorithm implementation

1. Enter the value of n in decimal:

n=int(input())

2. Use functions to find hexadecimal numbers

(1) A function named a is defined, which accepts an integer parameter n.

(2) Inside the function, a string m is defined, which contains the character representation of the hexadecimal number: ‘012345’, which is the character representation of the hexadecimal number 0 to 5.

(3) Initialize an empty string i for storing hexadecimal representation.

(4) Check if n is equal to 0, return the string ‘0’, because 0 is represented as 0 in hexadecimal.

(5) Use a while loop to continuously perform the following steps until n becomes 0: a. Calculate n and take the remainder of 6, and store the remainder in the variable liu. b. Take the hexadecimal character corresponding to liu from the string m and append it to the front of the string i. c. Divide n by 6 and take the integer part to update the value of n.

(6) Finally, when n becomes 0, i contains the hexadecimal representation of n.

code show as below:

def a(n):
    m = '012345'
    i = ''
    if n==0:
        return '0'
    while n:
        liu=n%6
        i=m[liu] + i
        n=n//6
    return i

3. Print the hexadecimal value

print(a(n))

4. All codes are as follows:

n=int(input())
def a(n):
    m = '012345'
    i = ''
    if n==0:
        return '0'
    while n:
        liu=n%6
        i=m[liu] + i
        n=n//6
    return i
print(a(n))

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 56903 people are learning the system