How to convert to different number systems. Converting numbers from one number system to another. Binary number system

Methods for converting numbers from one number system to another.

Converting numbers from one positional number system to another: converting integers.

To convert an integer from one number system with base d1 to another with base d2, you must sequentially divide this number and the resulting quotients by base d2 of the new system until you get a quotient less than base d2. The last quotient is the most significant digit of a number in the new number system with base d2, and the digits following it are remainders from division, written in the reverse order of their receipt. Perform arithmetic operations in the number system in which the number being translated is written.

Example 1. Convert the number 11(10) to the binary number system.

Answer: 11(10)=1011(2).

Example 2. Convert the number 122(10) to the octal number system.


Answer: 122(10)=172(8).

Example 3. Convert the number 500(10) to hexadecimal number system.


Answer: 500(10)=1F4(16).

Converting numbers from one positional number system to another: converting proper fractions.

To convert a proper fraction from a number system with base d1 to a system with base d2, it is necessary to sequentially multiply the original fraction and the fractional parts of the resulting products by the base of the new number system d2. The correct fraction of a number in the new number system with base d2 is formed in the form of integer parts of the resulting products, starting from the first.
If the translation results in a fraction in the form of an infinite or divergent series, the process can be completed when the required accuracy is achieved.

When translating mixed numbers, it is necessary to separately translate the integer and fractional parts into a new system according to the rules for translating integers and proper fractions, and then combine both results into one mixed number in the new number system.

Example 1. Convert the number 0.625(10) to the binary number system.


Answer: 0.625(10)=0.101(2).

Example 2. Convert the number 0.6(10) to the octal number system.


Answer: 0.6(10)=0.463(8).

Example 2. Convert the number 0.7(10) to hexadecimal number system.


Answer: 0.7(10)=0.B333(16).

Convert binary, octal and hexadecimal numbers to decimal number system.

To convert a number from the P-ary system to a decimal one, you must use the following expansion formula:
аnan-1…а1а0=аnPn+ аn-1Pn-1+…+ а1P+a0 .

Example 1. Convert the number 101.11(2) to the decimal number system.

Answer: 101.11(2)= 5.75(10) .

Example 2. Convert the number 57.24(8) to the decimal number system.

Answer: 57.24(8) = 47.3125(10) .

Example 3. Convert the number 7A,84(16) to the decimal number system.

Answer: 7A.84(16)= 122.515625(10) .


Converting octal and hexadecimal numbers to the binary number system and vice versa.

To convert a number from the octal number system to binary, each digit of this number must be written as a three-digit binary number (triad).

Example: write the number 16.24(8) in the binary number system.


Answer: 16.24(8)= 1110.0101(2) .

To convert a binary number back into the octal number system, you need to divide the original number into triads to the left and right of the decimal point and represent each group with a digit in the octal number system. Extreme incomplete triads are supplemented with zeros.

Example: write the number 1110.0101(2) in the octal number system.


Answer: 1110.0101(2)= 16.24(8) .

To convert a number from the hexadecimal number system to the binary system, you need to write each digit of this number as a four-digit binary number (tetrad).

Example: write the number 7A,7E(16) in the binary number system.


Answer: 7A,7E(16)= 1111010.0111111(2) .

Note: leading zeros on the left for integers and on the right for fractions are not written.

To convert a binary number back into the hexadecimal number system, you need to divide the original number into tetrads to the left and right of the decimal point and represent each group with a digit in the hexadecimal number system. Extreme incomplete triads are supplemented with zeros.

Example: write the number 1111010.0111111(2) in hexadecimal number system.

From 16 or 8 to 2

Translation octal And hexadecimal numbers to binary system very simple: just replace each digit with its binary equivalent triad(three digits) or notebook(four digits) (see table).
Binary (Radise 2) Octal (Base 8) Decimal (Base 10) Hexadecimal (Base 16)
triads tetrads
0 1 0 1 2 3 4 5 6 7 000 001 010 011 100 101 110 111 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 A B C D E F 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

For example:

a) Translate 305.4 8 "2" s.s.

b) Translate 7B2.E 16 "2" s.s.

16A 16 =1 0110 1010 2 345 8 =11 100 101 2

From 2 to 16 or 8

For example:

a) Translate 1101111001.1101 2 "8" s.s.

b) Translate 11111111011.100111 2 "16" s.s.

1000101010010101 2 =1000 1010 1001 0101=8A95 16 = 1 000 101 010 010 101=105225 8

From 16 to 8 and back

Conversion from octal to hexadecimal and back is carried out through the binary system using triads and tetrads.

For example:

Translate 175.24 8 "16" s.s.

Result: 175.24 8 = 7D.5 16.

From 10 to any s.s.

For example:

a) Translate 181 10 "8" s.s.

Result: 181 10 = 265 8

b) Translate 622 10 "16" s.s.

Result: 622 10 = 26E 16

Translation of proper fractions
To convert a regular decimal fraction to another system, this fraction must be sequentially multiplied by the base of the system into which it is converted. In this case, only fractional parts are multiplied. Fractions in the new system are written in the form of whole parts of products, starting from the first.

For example:

Convert 0.3125 10 "8" s.s.

Result: 0.3125 10 = 0.24 8

Comment. A final decimal fraction in another number system may correspond to an infinite (sometimes periodic) fraction. In this case, the number of characters in the representation of a fraction in the new system is taken depending on the required accuracy.

For example:

Convert 0.65 10 "2" s.s. Accuracy 6 digits.

Result: 0.65 10 0.10(1001) 2

To convert an improper decimal fraction to a number system with a non-decimal base It is necessary to translate the whole part and the fractional part separately.

For example:

Translate 23.125 10 "2" s.s.

Thus: 23 10 = 10111 2 ; 0.125 10 = 0.001 2.
Result: 23.125 10 = 10111.001 2.

It should be noted that integers remain integers, and proper fractions remain fractions in any number system.

From 2, 8 or 16 to 10

For example:

a)10101101.101 2 = 1 2 7 + 0 2 6 + 1 2 5 + 0 2 4 + 1 2 3 + 1 2 2 + 0 2 1 + 1 2 0 + 1 2 -1 + 0 2 -2 + 1 2 - 3 = 173.625 10

b) Translate 703.04 8 "10" s.s.

703.04 8 = 7 8 2 + 0 8 1 + 3 8 0 + 0 8 -1 + 4 8 -2 = 451.0625 10

c) Translate B2E.4 16 "10" s.s.

B2E.4 16 = 11 16 2 + 2 16 1 + 14 16 0 + 4 16 -1 = 2862.25 10

Scheme for converting numbers from one number system to another


Arithmetic operations in positional number systems

Let's look at the basic arithmetic operations: addition, subtraction, multiplication and division. The rules for performing these operations in the decimal system are well known - these are addition, subtraction, multiplication by a column and division by an angle. These rules apply to all other positional number systems. Only addition and multiplication tables must be used specific for each system.

Addition

When adding, the numbers are summed up by digits, and if there is an excess, it is transferred to the left

When adding binary numbers in each digit, the digits of the terms are added and transferred from the adjacent low-order digit, if any. It is necessary to take into account that 1+1 gives a zero in a given digit and a carry unit to the next one.

For example:

Perform addition of binary numbers:
a) X=1101, Y=101;

Result 1101+101=10010.

b) X=1101, Y=101, Z=111;

Result 1101+101+111=11001.

Addition table in the 8th number system

2+2=4 3+2=5 4+2=6 5+2=7 6+2=10 7+2=11
2+3=5 3+3=6 4+3=7 5+3=10 6+3=11 7+3=12
2+4=6 3+4=7 4+4=10 5+4=11 6+4=12 7+4=13
2+5=7 3+5=10 4+5=11 5+5=12 6+5=13 7+5=14
2+6=10 3+6=11 4+6=12 5+6=13 6+6=14 7+6=15
2+7=11 3+7=12 4+7=13 5+7=14 6+7=15 7+7=16

Addition table in the 16th number system

+ A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A B C D E F
A A B C D E F
B B C D E F 1A
C C D E F 1A 1B
D D E F 1A 1B 1C
E E F 1A 1B 1C 1D
F F 1A 1B 1C 1D 1E

To convert numbers from one number system to another, you must have basic information about number systems and the form of representation of numbers in them.

Quantity s The number of different digits used in a number system is called the base, or base of the number system. In general, a positive number X in a positional system with a base s can be represented as a polynomial:

Where s- base of the number system, - numbers allowed in a given number system. The sequence forms a whole part X, and the sequence is the fractional part X.

In computing, the most widely used are binary (BIN - binary) and binary coded number systems: octal (OCT - octal), hexadecimal (HEX - hexadecimal) and binary coded decimal (BCD - binary coded decimal).

In the future, to indicate the number system used, the number will be enclosed in brackets, and the base of the system will be indicated in the index. Number X based on s will be indicated .

Binary number system

The base of the number system is the number 2 ( s= 2) and only two digits are used to write numbers: 0 and 1. To represent any digit of a binary number, it is enough to have a physical element with two clearly different stable states, one of which represents 1 and the other 0.

Before you start converting from any number system to binary, you need to carefully study an example of writing a number in the binary number system:

If you do not need to go deep into the theory, but just need to get the result, then use Online calculator Converting integers from the decimal number system to other systems .

Octal and hexadecimal number systems

These number systems are binary-coded, in which the base of the number system is an integer power of two: - for octal and - for hexadecimal.

In the octal number system( s= 8) 8 digits are used: 0, 1, 2, 3, 4, 5, 6, 7.

Before you start converting from any number system to octal, you need to carefully study an example of writing a number in the octal system:

In hexadecimal number system ( s= 16) 16 digits are used: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.

An example of writing a number in hexadecimal:

The widespread use of octal and hexadecimal number systems is due to two factors.

Firstly, these systems allow you to replace the notation of a binary number with a more compact representation (the notation of a number in octal and hexadecimal systems will be 3 and 4 times shorter than the binary notation of this number, respectively). Secondly, the mutual conversion of numbers between the binary system on the one hand and the octal and hexadecimal systems on the other is relatively simple. Indeed, since for an octal number each digit is represented by a group of three binary digits (triads), and for a hexadecimal number - by a group of four binary digits (tetrads), then to convert a binary number it is enough to combine its digits into groups of 3 or 4 digits, respectively, moving from the comma to the right and left. In this case, if necessary, zeros are added to the left of the integer part and/or to the right of the fractional part and each such group - triad or tetrad - is replaced with an equivalent octal or hexadecimal digit (see table).

If you do not need to go deep into the theory, but just need to get the result, then use Online calculator Converting integers from the decimal number system to other systems .

Correspondence between digits in different number systems
DEC BIN OCT HEX BCD
0 0000 0 0 0000
1 0001 1 1 0001
2 0010 2 2 0010
3 0011 3 3 0011
4 0100 4 4 0100
5 0101 5 5 0101
6 0110 6 6 0110
7 0111 7 7 0111
8 1000 10 8 1000
9 1001 11 9 1001
10 1010 12 A 0001 0000
11 1011 13 B 0001 0001
12 1100 14 C 0001 0010
13 1101 15 D 0001 0011
14 1110 16 E 0001 0100
15 1111 17 F 0001 0101

For reverse translation, each OCT or HEX digit is replaced, respectively, by a triad or tetrad of binary digits, with insignificant zeros on the left and right being discarded.

For the examples discussed earlier, this looks like this:

If you do not need to go deep into the theory, but just need to get the result, then use Online calculator Converting integers from the decimal number system to other systems .

Binary decimal number system

In the BCD system, the weight of each digit is equal to a power of 10, as in the decimal system, and each decimal digit is encoded by four binary digits. To write a decimal number in the BCD system, it is enough to replace each decimal digit with an equivalent four-digit binary combination:

Any decimal number can be represented in BCD notation, but remember that it is not the binary equivalent of the number. This can be seen from the following example:

Converting numbers from one number system to another

Let X- a number in a number system with a base s, which needs to be represented in a system with a base h. It is convenient to distinguish two cases.

In the first case and, therefore, when moving to the base h you can use the arithmetic of this system. The conversion method consists of representing the number as a polynomial in powers s, as well as in the calculation of this polynomial according to the rules of arithmetic of the radix number system h. For example, it is convenient to switch from the binary or octal number system to the decimal number system. The described technique is illustrated by the following examples:

.

.

In both cases, arithmetic operations are performed according to the rules of the base 10 number system.

In the second case () it is more convenient to use radix arithmetic s. It should be taken into account here that the translation of integers and proper fractions is carried out according to different rules. When translating mixed fractions, the integer and fractional parts are each translated according to their own rules, after which the resulting numbers are written separated by commas.

Integer conversion

The rules for converting integers become clear from the general formula for writing a number in an arbitrary positional system. Let the number in the original number system s looks like . You need to get a number written in a number system with a base h:

.

To find the values, divide this polynomial by h:

.

As you can see, the least significant digit, that is, is equal to the first remainder. The next significant digit is determined by dividing the quotient by h:

.

The rest are also calculated by dividing the quotients until it equals zero.

To convert an integer from the s-ary number system to the h-ary number system, it is necessary to sequentially divide this number and the resulting quotients by h (according to the rules of the number system with base h) until the quotient becomes equal to zero. The most significant digit in the notation of a number with base h is the last remainder, and the digits following it form the remainders from previous divisions, written in the reverse order of their receipt.

1. Ordinal counting in various number systems.

In modern life, we use positional number systems, that is, systems in which the number denoted by a digit depends on the position of the digit in the notation of the number. Therefore, in the future we will talk only about them, omitting the term “positional”.

In order to learn how to convert numbers from one system to another, we will understand how sequential recording of numbers occurs using the example of the decimal system.

Since we have a decimal number system, we have 10 symbols (digits) to construct numbers. We start counting: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. The numbers are over. We increase the bit depth of the number and reset the low-order digit: 10. Then we increase the low-order digit again until all the digits are gone: 11, 12, 13, 14, 15, 16, 17, 18, 19. We increase the high-order digit by 1 and reset the low-order digit: 20. When we use all the digits for both digits (we get the number 99), we again increase the digit capacity of the number and reset the existing digits: 100. And so on.

Let's try to do the same in the 2nd, 3rd and 5th systems (we introduce the notation for the 2nd system, for the 3rd, etc.):

0 0 0 0
1 1 1 1
2 10 2 2
3 11 10 3
4 100 11 4
5 101 12 10
6 110 20 11
7 111 21 12
8 1000 22 13
9 1001 100 14
10 1010 101 20
11 1011 102 21
12 1100 110 22
13 1101 111 23
14 1110 112 24
15 1111 120 30

If the number system has a base greater than 10, then we will have to enter additional characters; it is customary to enter letters of the Latin alphabet. For example, for the 12-digit system, in addition to ten digits, we need two letters ( and ):

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10
11
12 10
13 11
14 12
15 13

2. Conversion from the decimal number system to any other.

To convert a positive integer decimal number to a number system with a different base, you need to divide this number by the base. Divide the resulting quotient by the base again, and further until the quotient is less than the base. As a result, write down in one line the last quotient and all remainders, starting from the last.

Example 1. Let's convert the decimal number 46 to the binary number system.

Example 2. Let's convert the decimal number 672 to the octal number system.

Example 3. Let's convert the decimal number 934 to the hexadecimal number system.

3. Conversion from any number system to decimal.

In order to learn how to convert numbers from any other system to decimal, let's analyze the usual notation for a decimal number.
For example, the decimal number 325 is 5 units, 2 tens and 3 hundreds, i.e.

The situation is exactly the same in other number systems, only we will multiply not by 10, 100, etc., but by the powers of the base of the number system. For example, let's take the number 1201 in the ternary number system. Let's number the digits from right to left starting from zero and imagine our number as the sum of the products of a digit and three to the power of the digit of the number:

This is the decimal notation of our number, i.e.

Example 4. Let's convert the octal number 511 to the decimal number system.

Example 5. Let's convert the hexadecimal number 1151 to the decimal number system.

4. Conversion from the binary system to the system with the base “power of two” (4, 8, 16, etc.).

To convert a binary number into a number with a power of two base, it is necessary to divide the binary sequence into groups according to the number of digits equal to the power from right to left and replace each group with the corresponding digit of the new number system.

For example, Let's convert the binary number 1100001111010110 to the octal system. To do this, we will divide it into groups of 3 characters starting from the right (since ), and then use the correspondence table and replace each group with a new number:

We learned how to build a correspondence table in step 1.

0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7

Those.

Example 6. Let's convert the binary number 1100001111010110 to hexadecimal.

0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

5. Conversion from a system with the base “power of two” (4, 8, 16, etc.) to binary.

This translation is similar to the previous one, done in the opposite direction: we replace each digit with a group of digits in the binary system from the correspondence table.

Example 7. Let's convert the hexadecimal number C3A6 to the binary number system.

To do this, replace each digit of the number with a group of 4 digits (since ) from the correspondence table, supplementing the group with zeros at the beginning if necessary:



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 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 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.

Share