• Conversion table from one number system to another. Converting numbers to binary, hexadecimal, decimal, octal number systems

    Note 1

    If you want to convert a number from one number system to another, then it is more convenient to first convert it to decimal system number system, and only then convert from decimal to any other number system.

    Rules for converting numbers from any number system to decimal

    IN computer technology, using machine arithmetic, an important role is played by the conversion of numbers from one number system to another. Below we give the basic rules for such transformations (translations).

      When transferring binary number in decimal it is required to represent a binary number as a polynomial, each element of which is represented as the product of a digit of the number and the corresponding power of the base number, in in this case$2$, and then you need to calculate the polynomial using the rules of decimal arithmetic:

      $X_2=A_n \cdot 2^(n-1) + A_(n-1) \cdot 2^(n-2) + A_(n-2) \cdot 2^(n-3) + ... + A_2 \cdot 2^1 + A_1 \cdot 2^0$

    Figure 1. Table 1

    Example 1

    Convert the number $11110101_2$ to the decimal number system.

    Solution. Using the given table of $1$ powers of the base $2$, we represent the number as a polynomial:

    $11110101_2 = 1 \cdot 27 + 1 \cdot 26 + 1 \cdot 25 + 1 \cdot 24 + 0 \cdot 23 + 1 \cdot 22 + 0 \cdot 21 + 1 \cdot 20 = 128 + 64 + 32 + 16 + 0 + 4 + 0 + 1 = 245_(10)$

      To convert a number from the octal number system to the decimal number system, you need to represent it as a polynomial, each element of which is represented as the product of a digit of the number and the corresponding power of the base number, in this case $8$, and then you need to calculate the polynomial according to the rules of decimal arithmetic:

      $X_8 = A_n \cdot 8^(n-1) + A_(n-1) \cdot 8^(n-2) + A_(n-2) \cdot 8^(n-3) + ... + A_2 \cdot 8^1 + A_1 \cdot 8^0$

    Figure 2. Table 2

    Example 2

    Convert the number $75013_8$ to the decimal number system.

    Solution. Using the given table of $2$ powers of the base $8$, we represent the number as a polynomial:

    $75013_8 = 7\cdot 8^4 + 5 \cdot 8^3 + 0 \cdot 8^2 + 1 \cdot 8^1 + 3 \cdot 8^0 = 31243_(10)$

      To convert a number from hexadecimal to decimal, you need to represent it as a polynomial, each element of which is represented as the product of a digit of the number and the corresponding power of the base number, in this case $16$, and then you need to calculate the polynomial according to the rules of decimal arithmetic:

      $X_(16) = A_n \cdot 16^(n-1) + A_(n-1) \cdot 16^(n-2) + A_(n-2) \cdot 16^(n-3) + . .. + A_2 \cdot 16^1 + A_1 \cdot 16^0$

    Figure 3. Table 3

    Example 3

    Convert the number $FFA2_(16)$ to the decimal number system.

    Solution. Using the given table of $3$ powers of the base $8$, we represent the number as a polynomial:

    $FFA2_(16) = 15 \cdot 16^3 + 15 \cdot 16^2 + 10 \cdot 16^1 + 2 \cdot 16^0 =61440 + 3840 + 160 + 2 = 65442_(10)$

    Rules for converting numbers from the decimal number system to another

    • To convert a number from the decimal number system to the binary system, it must be sequentially divided by $2$ until there is a remainder less than or equal to $1$. A number in the binary system is represented as a sequence of the last result of division and the remainders from division in reverse order.

    Example 4

    Convert the number $22_(10)$ to the binary number system.

    Solution:

    Figure 4.

    $22_{10} = 10110_2$

    • To convert a number from the decimal number system to octal, it must be sequentially divided by $8$ until there is a remainder less than or equal to $7$. A number in the octal number system is represented as a sequence of digits of the last division result and the remainders from the division in reverse order.

    Example 5

    Convert the number $571_(10)$ to the octal number system.

    Solution:

    Figure 5.

    $571_{10} = 1073_8$

    • To convert a number from the decimal number system to hexadecimal system it must be successively divided by $16$ until there is a remainder less than or equal to $15$. A number in the hexadecimal system is represented as a sequence of digits of the last division result and the remainder of the division in reverse order.

    Example 6

    Convert the number $7467_(10)$ to hexadecimal number system.

    Solution:

    Figure 6.

    $7467_(10) = 1D2B_(16)$

      In order to convert a proper fraction from a decimal number system to a non-decimal number system, it is necessary to sequentially multiply the fractional part of the number being converted by the base of the system to which it needs to be converted. Fraction in new system will be presented in the form of entire parts of works, starting with the first.

      For example: $0.3125_((10))$ in octal number system will look like $0.24_((8))$.

      In this case, you may encounter a problem when a finite decimal fraction can correspond to an infinite (periodic) fraction in the non-decimal number system. In this case, the number of digits in the fraction represented in the new system will depend on the required accuracy. It should also be noted that integers remain integers, and proper fractions remain fractions in any number system.

    Rules for converting numbers from a binary number system to another

    • To convert a number from the binary number system to octal, it must be divided into triads (triples of digits), starting with the least significant digit, if necessary, adding zeros to the leading triad, then replace each triad with the corresponding octal digit according to Table 4.

    Figure 7. Table 4

    Example 7

    Convert the number $1001011_2$ to the octal number system.

    Solution. Using Table 4, we convert the number from the binary number system to octal:

    $001 001 011_2 = 113_8$

    • To convert a number from the binary number system to hexadecimal, it should be divided into tetrads (four digits), starting with the least significant digit, if necessary, adding zeros to the most significant tetrad, then replace each tetrad with the corresponding octal digit according to Table 4.

    In this article I will cover the basics computer equipment is a binary system. This is the most low level, these are the numbers by which the computer works. And you will learn how to transfer from one system

    Table 1 - Representation of numbers in various systems
    calculus (beginning)

    Number systems

    Decimal

    Binary

    Octal

    Hexadecimal

    BCD

    To convert from decimal to binary, you have two options.

    1) For example, the number 37 needs to be converted from the decimal system to the binary system, then you need to divide it by two, and then check the remainder of the division. If the remainder is odd, then we write one at the bottom and the next division cycle goes through an even number; if the remainder of the division is even, then we write zero. At the end you must get 1. And now we convert the resulting result into binary, and the number goes from right to left.

    Step by step: 37 is an odd number, which means 1 , then 36/2 = 18. The number is even, which means 0. 18/2 = 9 is an odd number, which means 1 , then 8/2 = 4. The number is even, read 0. 4/2 = 2, an even number means 0, 2/2 = 1.

    So we got the number. Don't forget to count from right to left: 100101 - now we have a number in the binary system. In general, this is written as a division in a column, as you see in the figure below:

    2) But there is a second way. I like him better. Transfer from one system to another is as follows:

    where ai - i-th digit numbers;
    k - the number of digits in the fractional part of the number;
    m - the number of digits in the integer part of the number;
    N is the base of the number system.

    The base of the number system N shows how many times the “weight” of the i-th digit is greater than the “weight” (i-1) of the digit. The integer part of a number is separated from the fractional part by a dot (comma).

    The integer part of the number AN1, with the base N1, is converted to the number system with the base N2 by sequentially dividing the integer part of the number AN1 by the base N2 written as a number with the base N1, until a remainder is obtained. The resulting part is again divided by the base N2, and this process must repeat until the particle becomes smaller than the divisor. The resulting remainders from division and the last part are written in the reverse order obtained during division. The generated number will be an integer with base N2.

    The fractional part of the number AN1, with base N1, is converted into a number system with base N2 by sequentially multiplying the fractional part of the number AN1 by base N2, written as a number with base N1. With each multiplication, the integer part of the product is taken in the form of the next digit of the corresponding digit, and the fractional part of the remaining is taken as a new multiplication. The number of multiplications determines the digit capacity of the resulting result, representing the fractional part of the number AN1 in the N2 number system. The fractional part of a number is often represented inaccurately when translated.

    Let's do this with an example:

    Convert from decimal to binary

    37 in decimal must be converted to binary. Let's work with degrees:

    2 0 = 1
    2 1 = 2
    2 2 = 4
    2 3 = 8
    2 4 = 16
    2 5 = 32
    2 6 = 64
    2 7 = 128
    2 8 = 256
    2 9 = 512
    2 10 = 1024 and so on... ad infinitum

    This means: 37 - 32 = 5. 5 - 4 = 1. The answer is as follows in binary: 100101.

    Let's convert the number 658 from decimal to binary:

    658-512=146
    146-128=18
    18-16=2. In the binary system the number will look like: 1010010010.

    Converting from decimal to octal

    If you need to convert from decimal to octal, you must first convert to binary, and then convert from binary to octal. That is, it’s easier this way, although you can translate it right away. Using an algorithm similar to the one for converting to binary, see above.

    Convert from decimal to hexadecimal

    If you need to convert from decimal to hexadecimal, you must first convert to binary and then convert from binary to hexadecimal. That is, it’s easier this way, although you can translate it right away. Using an algorithm similar to the one for converting to binary, see above.

    Converting from binary to octal

    To convert a number from binary to octal, you need to split the binary into three numbers.

    For example, the resulting number 1010010010 is divided into three numbers, and the division goes from right to left: 1,010,010,010 = 1222. See the table at the very beginning.

    Converting from binary to hexadecimal

    To convert a number from binary to hexadecimal, you need to divide it into tetrads (four each)

    10 1001 0010 = 292

    Here are a few examples for you to look through:

    Conversion is from binary to octal, then to hexadecimal, and then from binary to decimal

    (2) = 11101110
    (8) = 11 101 110 = 276
    (16) = 1110 1110 = EE
    (10) = 1*128+ 1*64+ 1*32+ 0 +1*8 + 1*4 + 1*2+ 0= 238
    3) (8) = 657

    Conversion is carried out from hexadecimal to binary, then to octal, and then from binary to decimal

    (16) = 6E8
    (2) = 110 1110 1000
    (8) = 11 011 101 000 = 2250
    (10) = 1*1024+1*512+ 0 +1*128+ 1*64+ 1*32+ 8 = 1768

    Converting numbers from one number system to another is an important part of machine arithmetic. Let's consider the basic rules of translation.

    1. To convert a binary number to a decimal one, it is necessary to write it in the form of a polynomial consisting of the products of the digits of the number and the corresponding power of 2, and calculate it according to the rules of decimal arithmetic:

    When translating, it is convenient to use the table of powers of two:

    Table 4. Powers of number 2

    Those taking the Unified State Exam and more...

    It is strange that in computer science lessons in schools they usually show students the most complex and inconvenient way to convert numbers from one system to another. This method consists of sequentially dividing the original number by the base and collecting the remainders from the division in reverse order.

    For example, you need to convert the number 810 10 to binary:

    We write the result in reverse order from bottom to top. It turns out 81010 = 11001010102

    If you need to convert to the binary system, quite big numbers, then the division ladder takes on the size of a multi-story building. And how can you collect all the ones and zeros and not miss a single one?

    IN Unified State Exam program in computer science includes several tasks related to the translation of numbers from one system to another. Typically, this is a conversion between octal and hexadecimal systems and binary. These are sections A1, B11. But there are also problems with other number systems, such as in section B7.

    To begin with, let us recall two tables that would be good to know by heart for those who choose computer science as their future profession.

    Table of powers of number 2:

    2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10
    2 4 8 16 32 64 128 256 512 1024

    It is easily obtained by multiplying the previous number by 2. So, if you do not remember all of these numbers, it is not difficult to obtain the rest in your mind from those that you remember.

    Table of binary numbers from 0 to 15 with hexadecimal representation:

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
    0 1 2 3 4 5 6 7 8 9 A B C D E F

    The missing values ​​are also easy to calculate by adding 1 to the known values.

    Integer conversion

    So, let's start by converting directly to the binary system. Let's take the same number 810 10. We need to decompose this number into terms equal to powers of two.

    1. We are looking for the power of two closest to 810 and not exceeding it. This is 2 9 = 512.
    2. Subtract 512 from 810, we get 298.
    3. Repeat steps 1 and 2 until there are no 1s or 0s left.
    4. We got it like this: 810 = 512 + 256 + 32 + 8 + 2 = 2 9 + 2 8 + 2 5 + 2 3 + 2 1.
    Then there are two methods, you can use any of them. How easy it is to see that in any number system its base is always 10. The square of the base will always be 100, the cube 1000. That is, the degree of the base of the number system is 1 (one), and there are as many zeros behind it as the degree is.

    Method 1: Arrange 1 according to the ranks of the indicators of the terms. In our example, these are 9, 8, 5, 3 and 1. The remaining places will contain zeros. So, we got the binary representation of the number 810 10 = 1100101010 2. Units are placed in 9th, 8th, 5th, 3rd and 1st places, counting from right to left from zero.

    Method 2: Let's write the terms as powers of two under each other, starting with the largest.

    810 =

    Now let's add these steps together, like folding a fan: 1100101010.

    That's it. Along the way, the problem of “how many units in binary notation number 810?

    The answer is as many as there are terms (powers of two) in this representation. 810 has 5 of them.

    Now the example is simpler.

    Let's convert the number 63 to the 5-ary number system. The closest power of 5 to 63 is 25 (square 5). A cube (125) will already be a lot. That is, 63 lies between the square of 5 and the cube. Then we will select the coefficient for 5 2. This is 2.

    We get 63 10 = 50 + 13 = 50 + 10 + 3 = 2 * 5 2 + 2 * 5 + 3 = 223 5.

    And, finally, very easy translations between 8 and hexadecimal systems. Since their base is a power of two, the translation is done automatically, simply by replacing the numbers with their binary representation. For the octal system, each digit is replaced by three binary digits, and for the hexadecimal system, four. In this case, all leading zeros are required, except for the most significant digit.

    Let's convert the number 547 8 to binary.

    547 8 = 101 100 111
    5 4 7

    One more, for example 7D6A 16.

    7D6A 16 = (0)111 1101 0110 1010
    7 D 6 A

    Let's convert the number 7368 to the hexadecimal system. First, write the numbers in triplets, and then divide them into quadruples from the end: 736 8 = 111 011 110 = 1 1101 1110 = 1DE 16. Let's convert the number C25 16 to the octal system. First, we write the numbers in fours, and then divide them into threes from the end: C25 16 = 1100 0010 0101 = 110 000 100 101 = 6045 8. Now let's look at converting back to decimal. It is not difficult, the main thing is not to make mistakes in the calculations. We expand the number into a polynomial with powers of the base and coefficients for them. Then we multiply and add everything. E68 16 = 14 * 16 2 + 6 * 16 + 8 = 3688. 732 8 = 7 * 8 2 + 3*8 + 2 = 474 .

    Converting Negative Numbers

    Here you need to take into account that the number will be presented in additional code. To convert a number into additional code, you need to know the final size of the number, that is, what we want to fit it into - in a byte, in two bytes, in four. The most significant digit of a number means the sign. If there is 0, then the number is positive, if 1, then it is negative. On the left, the number is supplemented with a sign digit. We do not consider unsigned numbers; they are always positive, and the most significant bit in them is used as information.

    For translation negative number in binary's complement code you need to convert a positive number to binary, then change the zeros to ones and the ones to zeros. Then add 1 to the result.

    So, let's convert the number -79 to the binary system. The number will take us one byte.

    We convert 79 to the binary system, 79 = 1001111. We add zeros on the left to the size of the byte, 8 bits, we get 01001111. We change 1 to 0 and 0 to 1. We get 10110000. We add 1 to the result, we get the answer 10110001. Along the way, we answer the Unified State Exam question “how many units are in the binary representation of the number -79?” The answer is 4.

    Adding 1 to the inverse of a number eliminates the difference between the representations +0 = 00000000 and -0 = 11111111. In two's complement code they will be written the same as 00000000.

    Converting fractional numbers

    Fractional numbers are converted in the reverse way of dividing whole numbers by the base, which we looked at at the very beginning. That is, using sequential multiplication by a new base with the collection of whole parts. The integer parts obtained during multiplication are collected, but do not participate in the following operations. Only fractions are multiplied. If the original number is greater than 1, then the integer and fractional parts are translated separately and then glued together.

    Let's convert the number 0.6752 to the binary system.

    0 ,6752
    *2
    1 ,3504
    *2
    0 ,7008
    *2
    1 ,4016
    *2
    0 ,8032
    *2
    1 ,6064
    *2
    1 ,2128

    The process can be continued for a long time until we get all the zeros in the fractional part or the required accuracy is achieved. Let's stop at the 6th sign for now.

    It turns out 0.6752 = 0.101011.

    If the number was 5.6752, then in binary it will be 101.101011.

    2.3. Converting numbers from one number system to another

    2.3.1. Converting integers from one number system to another

    It is possible to formulate an algorithm for converting integers from a radix system p into a system with a base q :

    1. Express the base of the new number system with numbers from the original number system and carry out all subsequent actions in the original number system.

    2. Consistently divide the given number and the resulting integer quotients by the base of the new number system until we obtain a quotient that is smaller than the divisor.

    3. The resulting remainders, which are digits of the number in the new number system, are brought into accordance with the alphabet of the new number system.

    4. Compose a number in the new number system, writing it starting from the last remainder.

    Example 2.12. Convert the decimal number 173 10 to octal number system:

    We get: 173 10 =255 8

    Example 2.13. Convert the decimal number 173 10 to hexadecimal number system:

    We get: 173 10 =AD 16.

    Example 2.14. Convert the decimal number 11 10 to the binary number system. It is more convenient to depict the sequence of actions discussed above (translation algorithm) as follows:

    We get: 11 10 =1011 2.

    Example 2.15. Sometimes it is more convenient to write down the translation algorithm in table form. Let's convert the decimal number 363 10 to a binary number.

    Divider

    We get: 363 10 =101101011 2

    2.3.2. Converting fractional numbers from one number system to another

    It is possible to formulate an algorithm for converting a proper fraction with a base p into a fraction with a base q:

    1. Express the base of the new number system with numbers from the original number system and carry out all subsequent actions in the original number system.

    2. Consistently multiply the given numbers and the resulting fractional parts of the products by the base of the new system until the fractional part of the product becomes equal to zero or the required accuracy of number representation is achieved.

    3. The resulting integer parts of the products, which are digits of the number in the new number system, should be brought into conformity with the alphabet of the new number system.

    4. Compose the fractional part of a number in the new number system, starting from the integer part of the first product.

    Example 2.17. Convert the number 0.65625 10 to the octal number system.

    We get: 0.65625 10 =0.52 8

    Example 2.17. Convert the number 0.65625 10 to hexadecimal number system.

    x 16

    We get: 0.65625 10 =0.A8 1

    Example 2.18. Convert the decimal fraction 0.5625 10 to the binary number system.

    x 2

    x 2

    x 2

    x 2

    We get: 0.5625 10 =0.1001 2

    Example 2.19. Convert the decimal fraction 0.7 10 to the binary number system.

    Obviously, this process can continue indefinitely, giving more and more new signs in the image of the binary equivalent of the number 0.7 10. So, in four steps we get the number 0.1011 2, and in seven steps the number 0.1011001 2, which is a more accurate representation of the number 0.7 10 in binary number system, and etc. Such an endless process is terminated at a certain step, when it is considered that the required accuracy of number representation has been obtained.

    2.3.3. Translation of arbitrary numbers

    Translation of arbitrary numbers, i.e. numbers containing an integer and a fractional part are carried out in two stages. The integer part is translated separately, and the fractional part separately. In the final recording of the resulting number, the integer part is separated from the fractional part by a comma (dot).

    Example 2.20. Convert the number 17.25 10 to the binary number system.

    We get: 17.25 10 =1001.01 2

    Example 2.21. Convert the number 124.25 10 to octal system.

    We get: 124.25 10 =174.2 8

    2.3.4. Converting numbers from base 2 to base 2 n and vice versa

    Translation of integers. If the base of the q-ary number system is a power of 2, then the conversion of numbers from the q-ary number system to the 2-ary number system and back can be carried out using more simple rules. In order to write an integer binary number in the number system with base q=2 n, you need:

    1. Divide the binary number from right to left into groups of n digits each.

    2. If the last left group has less than n digits, then it must be supplemented on the left with zeros until the required number discharges.

    Example 2.22. The number 101100001000110010 2 will be converted to the octal number system.

    We divide the number from right to left into triads and under each of them write the corresponding octal digit:

    We get the octal representation of the original number: 541062 8 .

    Example 2.23. The number 1000000000111110000111 2 will be converted to the hexadecimal number system.

    We divide the number from right to left into tetrads and under each of them write the corresponding hexadecimal digit:

    We get the hexadecimal representation of the original number: 200F87 16.

    Converting fractional numbers. In order to write a fractional binary number in a number system with base q=2 n, you need:

    1. Divide the binary number from left to right into groups of n digits each.

    2. If the last right group has less than n digits, then it must be supplemented on the right with zeros to the required number of digits.

    3. Consider each group as an n-bit binary number and write it with the corresponding digit in the number system with the base q=2 n.

    Example 2.24. The number 0.10110001 2 will be converted to the octal number system.

    We divide the number from left to right into triads and under each of them we write the corresponding octal digit:

    We get the octal representation of the original number: 0.542 8 .

    Example 2.25. The number 0.100000000011 2 will be converted to the hexadecimal number system. We divide the number from left to right into tetrads and under each of them write the corresponding hexadecimal digit:

    We get the hexadecimal representation of the original number: 0.803 16

    Translation of arbitrary numbers. In order to write an arbitrary binary number in the number system with the base q=2 n, you need:

    1. Divide the integer part of a given binary number from right to left, and the fractional part from left to right into groups of n digits each.

    2. If the last left and/or right groups contain less than n digits, then they must be supplemented on the left and/or right with zeros to the required number of digits;

    3. Consider each group as an n-bit binary number and write it with the corresponding digit in the number system with the base q = 2 n

    Example 2.26. Let's convert the number 111100101.0111 2 to the octal number system.

    We divide the integer and fractional parts of the number into triads and under each of them write the corresponding octal digit:

    We get the octal representation of the original number: 745.34 8 .

    Example 2.27. The number 11101001000,11010010 2 will be converted to the hexadecimal number system.

    We divide the integer and fractional parts of the number into notebooks and under each of them write the corresponding hexadecimal digit:

    We get the hexadecimal representation of the original number: 748,D2 16.

    Converting numbers from number systems with base q=2n to binary. In order to convert an arbitrary number written in the number system with the base q=2 n into the binary number system, you need to replace each digit of this number with its n-digit equivalent in the binary number system.

    Example 2.28.Let's convert the hexadecimal number 4AC35 16 to the binary number system.

    According to the algorithm:

    We get: 1001010110000110101 2 .

    Tasks for independent completion (Answers)

    2.38. Fill out the table, in each row of which the same integer must be written in different number systems.

    Binary

    Octal

    Decimal

    Hexadecimal

    2.39. Fill out the table, in each row of which the same fractional number must be written in different number systems.

    Binary

    Octal

    Decimal

    Hexadecimal

    2.40. Fill out the table, in each row of which the same arbitrary number (the number can contain both an integer and a fractional part) should be written in different number systems.

    Binary

    Octal

    Decimal

    Hexadecimal

    59.B

    n (degree)

    Example.

    2. For translation octal number in decimal it is necessary to write it in the form of a polynomial, consisting of the products of the digits of the number and the corresponding power of the number 8, and calculate it according to the rules of decimal arithmetic:

    When translating, it is convenient to use the table of powers of eight:

    Table 5. Powers of the number 8

    n (degree)

    Example. Convert the number to the decimal number system.

    3. For translation hexadecimal number in decimal it must be written down as a polynomial, consisting of the products of the digits of the number and the corresponding power of the number 16, and calculated according to the rules of decimal arithmetic:

    When translating, it is convenient to use blitz of powers of number 16:

    Table 6. Powers of the number 16

    n (degree)

    Example. Convert the number to the decimal number system.

    4. For translation decimal number in the binary system, it must be successively divided by 2 until there remains a remainder less than or equal to 1. A number in the binary system is written as a sequence of the last division result and the remainders from the division in reverse order.

    Example. Convert the number to the binary number system.

    5. To convert a decimal number to the octal system, it must be sequentially divided by 8 until a remainder less than or equal to 7 remains. A number in the octal system is written as a sequence of digits of the last division result and the remainder of the division in reverse order.

    Example. Convert the number to the octal number system.

    6. To convert a decimal number to the hexadecimal system, it must be sequentially divided by 16 until there remains a remainder less than or equal to 15. A number in the hexadecimal system is written as a sequence of digits of the last division result and the remainders from the division in reverse order.

    Example. Convert the number to hexadecimal number system.