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. Base new system express numbers using 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 down starting from the last remainder.

Example 2.12. Translate decimal number 173 10 in 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 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 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 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

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 into 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 fairly large numbers into the binary system, 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?

The Unified State Exam program in computer science includes several tasks related to converting 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. At the same time, the problem “how many units are in the binary notation of the number 810?” is also simply solved.

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 two's complement 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.

To convert a negative number to binary's complement, 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 Unified State Exam question“how many ones 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.

Rule. To convert a number from one number system to another, you need to divide the original number by the base of the new number system. Divide the resulting quotient again by the base of the new number system, and continue the division until then. until the quotient is less than the base of the new number system. The resulting division remainders, starting from the last, are written in reverse order. This will be the recording of the number in the new number system.

Example. Convert the number 135 from decimal SS to 2-ary, octal and hexadecimal number systems.

1) 2) 3)

Task 2.

Convert the following numbers to binary, octal and hexadecimal SS: 1275,973, 172

Reverse conversion of numbers from any SS to decimal.

1) To convert a number from any SS to the original SS (reverse translation), you need to multiply each digit of this number by the base of the original SS. starting from the zero digit from right to left, and add the products. If you are converting a decimal fraction, you should apply the rule for writing the integer and fractional parts of the number.

2) Reverse translation of numbers is carried out according to the formula:

where A is a given number,

g – SS base of a given number (=2 for 2-ary SS, for other SS - similar),

m – the number of digits in the integer part of the number.

n – number of digits in the fractional part of the number,

a – the value of the digits of a given number (the fractional part of the number is highlighted in blue).

110110 2 = 1*2 5 +1*2 4 +0*2 3 +1*2 2 +1*2 1 +0*2 0 =54 10

66 8 =6*8 1 +6*8 0 =48+6=54 10 9A 16 =9*16 1 +10*16 0 =144+10=154 10

13.4 8 =1*8 1 +3*8 0 +4*8 -1 =8+3+0.5=11.5 10 (this number is a decimal fraction)

Task 3.

Convert the following numbers to decimal SS:

101,11 2 =5,75 10 1011001 2 1011,101 2

125,7 8 =86 10 1253 8 175,132 8

A19BA 16 =2585726… 10 16A3 16 2BAFD 16

Translation of numbers with a base that is a power of 2 and reverse translation. These systems include binary, octal, and hexadecimal number systems.



Rule. Convert from binary SS to octal SS. The binary number is divided into groups of 3 digits from the end (from right to left) and each group is converted into a number in a new SS

10.000.101 2 =205 8

111.000.101.100 2 =7054 8

1.011.001.101 2 =1315 8

Rule. For the reverse conversion, each octal digit is written as a triad.

Rule. From binary SS to hexadecimal SS: similar, but separate 4 digits each

0110.0110.1011 2 =66B 16

1011.1111.0111 2 =BF7 16

10.1010.0111.0001 2 =2A71 16

Rule. For the reverse conversion, each hexadecimal digit is written as a tetrad.

Translation of proper and improper fractions in different SS. If you need to translate common fraction, then you first need to convert it to a decimal fraction, and then apply the translation rules decimals.

Rule. Converting decimal fractions less than one ( proper fractions).

1) it is necessary to separate the fractional part with a vertical line;

2) multiply the fractional part based on the new number system;

3) write the result strictly under the original number, starting from the least significant digit; if you get a transfer to an entire part, then write it to the left of the line;

4) multiplication of the fractional part is carried out until a number with the specified accuracy is obtained, or there is no 0 to the right of the line.

0,728 10 =0,564 8

Task 4. Convert the following proper fractions from decimal SS to binary, octal, hexadecimal SS: .

To quickly convert numbers from the decimal number system to the binary system, you need to have a good knowledge of the numbers “2 to the power”. For example, 2 10 =1024, etc. This will allow you to solve some translation examples literally in seconds. One of these tasks is Problem A1 from the USE demo 2012. You can, of course, take a long and tedious time to divide a number by “2”. But it’s better to decide differently, saving valuable time on the exam.

The method is very simple. Its gist is this: If the number that needs to be converted from the decimal system is equal to the number "2 to the power", then this number in the binary system contains a number of zeros equal to the power. We add a “1” in front of these zeros.

  • Let's convert the number 2 from the decimal system. 2=2 1 . Therefore, in the binary system, a number contains 1 zero. We put “1” in front and get 10 2.
  • Let's convert 4 from the decimal system. 4=2 2 . Therefore, in the binary system, a number contains 2 zeros. We put “1” in front and get 100 2.
  • Let's convert 8 from the decimal system. 8=2 3 . Therefore, in the binary system, a number contains 3 zeros. We put “1” in front and get 1000 2.


Similarly for other numbers "2 to the power".

If the number to be translated is less number“2 to the power” by 1, then in the binary system this number consists only of units, the number of which is equal to the power.

  • Let's convert 3 from the decimal system. 3=2 2 -1. Therefore, in the binary system, a number contains 2 ones. We get 11 2.
  • Let's convert 7 from the decimal system. 7=2 3 -1. Therefore, in the binary system, a number contains 3 ones. We get 111 2.

In the figure, the squares indicate the binary representation of the number, and the pink color on the left indicates the decimal representation.


The translation is similar for other numbers “2 to the power-1”.

It is clear that the translation of numbers from 0 to 8 can be done quickly or by division, or simply know by heart their representation in the binary system. I gave these examples so that you understand the principle this method and used it to translate more "impressive numbers", for example, to translate the numbers 127,128, 255, 256, 511, 512, etc.

You can come across such problems when you need to convert a number that is not equal to the number “2 to the power”, but close to it. It may be greater or less than 2 to the power. The difference between the translated number and the number "2 to the power" should be small. For example, up to 3. The representation of numbers from 0 to 3 in the binary system just needs to be known without translation.

If the number is greater than , then solve like this:

First we convert the number “2 to the power” into the binary system. And then we add to it the difference between the number “2 to the power” and the number being translated.

For example, let's convert 19 from the decimal system. It is greater than the number "2 to the power" by 3.

16=2 4 . 16 10 =10000 2 .

3 10 =11 2 .

19 10 =10000 2 +11 2 =10011 2 .

If the number is less than the number "2 to the power", then it is more convenient to use the number "2 to the power-1". We solve it like this:

First we convert the number “2 to the power-1” into the binary system. And then we subtract from it the difference between the number “2 to the power of 1” and the number being translated.

For example, let's convert 29 from the decimal system. It is greater than the number “2 to the power-1” by 2. 29=31-2.

31 10 =11111 2 .

2 10 =10 2 .

29 10 =11111 2 -10 2 =11101 2

If the difference between the number being translated and the number "2 to the power" is more than three, then you can break the number into its components, convert each part into the binary system and add.

For example, convert the number 528 from the decimal system. 528=512+16. We translate 512 and 16 separately.
512=2 9 . 512 10 =1000000000 2 .
16=2 4 . 16 10 =10000 2 .
Now let's add it in a column:

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

We figured out the binary system, remembered the basic points, now let's talk about the hexadecimal system. The alphabet of the hexadecimal number system consists of sixteen different characters: 10 Arabic numerals (from 0 to 9) and 6 first capital 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, it is necessary to divide the source binary code into groups of four bits, and, which is very important, the division must begin from right to left.

101 . 0111 . 1100 . 1010

After splitting, we got four groups: 101, 0111, 1100 and 1010. Special attention requires the leftmost segment, 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 supplement this segment with a leading insignificant 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. Consequently, we have every right to 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.

At the final stage of the conversion, 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 extremely important topic, which forms the foundation used throughout the course.