Joke Collection Website - Mood Talk - C language hexadecimal conversion

C language hexadecimal conversion

The commonly used number systems in computers are: binary, octal, decimal, and hexadecimal. ?

Binary system uses two Arabic numerals: 0, 1;

Octal system uses eight Arabic numerals: 0, 1, 2, 3, 4, 5 , 6, 7;?

Decimal system uses ten Arabic numerals: 0 to 9;

Hexadecimal system is to enter 1 every 16, but we only have 0~9 Ten numbers, so we use the five letters A, B, C, D, E, and F to represent 10, 11, 12, 13, 14, and 15 respectively. Letters are not case sensitive. ?

The following uses the remainder short division method to convert decimal numbers into binary numbers as an example to illustrate

1. Clarify the problem. For example, we now want to convert a decimal number 156 into a binary number. First write this decimal number as the dividend in an inverted "long division" symbol. Write the base of the target number system (in this case "2" in binary) as the divisor outside the division symbol. Visualizing the calculation process in this way is easier to understand because the entire calculation process only requires dividing the number by 2.

2. Perform division operation. Write the integer part of the result (the quotient) below the long division symbol, and then write its remainder (0 or 1) to the right of the dividend.

We are now dividing by 2, so if the quotient is an even number, the remainder will be 0; if the quotient is an odd number, the remainder will be recorded as 1.

3. Keep dividing until the quotient is 0. Divide each new quotient by two and write the remainder to the right of the dividend. until the quotient reaches 0.

4. Write new binary numbers. Starting with the remainder at the bottom, read to the top in order. In this example, you will get 10011100. This is the binary form of the decimal number 156. Or, we can express it in the form of a footnote equation, that is: 15610?= 100111002

Using this method, we can convert all decimal numbers into any base expression. The divisor is 2 because we ultimately want a base 2 number (i.e. a binary value). If you eventually want to get numbers in other number systems, just replace the binary base 2 in this method with the base of the target number system. For example, to get a base 9 number, use 9 as the divisor instead of 2. The final result is the numerical expression of the target number system.

Extended information:

Decimal --->Binary

For the integer part, use the dividend to divide by 2 repeatedly, except for the first time. Use 2 to 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.

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 the "base multiplication method"

Decimal --->Octal

The method of converting decimal numbers into octal numbers, and converting them into binary numbers The method is similar, the only change is: the divisor changes from 2 to 8.

Reference material: Baidu Encyclopedia - Base conversion