Convert numbers from binary system online. Number systems

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 using the numbers of the original number system and carry out all subsequent actions in original system Reckoning.

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 the digits of the number in new system numbers, bring them into line with the alphabet of the new number system.

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

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

We get: 173 10 =255 8

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

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 will be 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 fractional part numbers 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 the binary number system, etc. Such an endless process is terminated at some step, when it is believed 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 to the required number of digits.

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 have 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 arbitrary number, written in the number system with the base q=2 n, converted to 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 various systems Reckoning.

Binary

Octal

Decimal

Hexadecimal

2.39. Fill out the table with the same thing in each row a 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) must be written in different number systems.

Binary

Octal

Decimal

Hexadecimal

59.B

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 it 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 division result and the remainders from the 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 the 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. Fractions in the new system will be represented as whole parts of products, 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 binary system numbering into 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.

Are there any difficulties or misunderstandings with converting numbers from binary to hexadecimal? Sign up with me for individual lessons in computer science and ICT. In our private lessons, my students and I analyze not only the theoretical part, but also solve a colossal number of different thematic exercises.

You need to know what a binary or binary number system is

Before thinking about how to convert a number from 2 to 16, you need to have a good understanding of what numbers are in the binary number system. Let me remind you that the alphabet of the binary number system consists of two valid elements - 0 And 1 . This means that absolutely any number written in binary, will consist of a set of zeros and ones. Here are examples of numbers written in binary representation: 10010, 100, 111101010110, 1000001.

You need to know what the hexadecimal number system is

WITH binary system We figured it out, remembered the basic points, now let's talk about the hexadecimal system. The hexadecimal alphabet consists of sixteen different characters: 10 Arabic numerals (from 0 to 9) and 6 initial capitals Latin letters(from "A" to "F"). This means that absolutely any number written in hexadecimal will consist of characters from the above alphabet. Here are examples of numbers written in hexadecimal notation:

810AFCDF198303 100FFF0

Let's talk about the algorithm for converting a number from 2 to hexadecimal number system

We will need in mandatory consider the Tetrad coding table. Without using this table, it will be quite difficult to quickly convert numbers from 2 to 16 system.

The purpose of the Tetrad encoding table is to uniquely match the symbols of the binary number system and the hexadecimal number system.

The Tetrad table has the following structure:

Tetrad table

0000 - 0

0001 - 1

0010 - 2

0011 - 3

0100 - 4

0101 - 5

0110 - 6

0111 - 7

1000 - 8

1001 - 9

1010 - A

1011 - B

1100 - C

1101 - D

1110 - E

1111 - F

Let's say we need to convert the number 101011111001010 2 to hexadecimal. First of all, you need the original binary code divided into groups of four categories, and, which is very important, the division must necessarily begin from right to left.

101 . 0111 . 1100 . 1010

After splitting, we received four groups: 101, 0111, 1100 and 1010. The leftmost segment requires special attention, that is, segment 101. As you can see, its length is 3 digits, and it is necessary that its length be equal to four, therefore, we will add this segment leading zero:

101 -> 0 101.

Tell me, on what basis do we add some 0 to the left of the number? The thing is that adding insignificant zeros does not have any effect on the value of the original number. Therefore we have every right add not only one zero to the left of a binary number, but in principle any number of zeros and get a number of the required length.

On final stage transformation, it is necessary to convert each of the resulting binary groups into the corresponding value according to the Tetrad coding table.

0101 -> 5 0111 -> 7 1100 -> C 1010 -> A

101011111001010 2 = 57CA 16

And now I suggest you familiarize yourself with the multimedia solution, which shows how it is converted from a binary state to a hexadecimal state:

Brief conclusions

In this short article we discussed the topic “ Number systems: how to convert from 2 to 16" If you have any questions or misunderstandings, please call and sign up for my individual lessons in computer science and programming. I will offer you to solve dozens of similar exercises and you will not have a single question left. In general, number systems are an extremely important topic that forms the foundation used throughout the course.

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, the rest are not difficult to obtain 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 all. 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 translate negative number needs to be converted to binary's complement positive number into the binary system, then change zeros to ones and 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.

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

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 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 16, and calculate it 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 sequentially 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 is 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.