# Converting numbers from one number system to another

Pin
Send
Share
Send
Send

The microprocessor uses a binary number system to represent numbers.
Moreover, any digital signal can have two stable states: “high level” and “low level”. In the binary system, two numbers are used for the image of any number, respectively: 0 and 1. An arbitrary number x = anan-1..a1a0, a-1a-2... a-m written in binary notation as

Where ai - binary digits (0 or 1).

In the hexadecimal notation, the base digits are digits from 0 to 15 inclusive. To designate basic digits greater than 9 with one character, in addition to the Arabic numerals 0 ... 9, the hexadecimal number system uses the letters of the Latin alphabet:

For example, the number 17510 in hexadecimal notation will be written as AF16. Really,

10·16 1 +15·16 0 =160+15=175

The table shows numbers from 0 to 16 in decimal, binary, octal and hexadecimal notation.

 Decimal Binary Octal Hexadecimal 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 16 10000 20 10

The binary number system is convenient for performing arithmetic operations using microprocessor hardware, but is inconvenient for human perception, since it requires a large number of digits. Therefore, in computer technology, in addition to the binary number system, octal and hexadecimal number systems are widely used for a more compact representation of numbers.

Three digits of the octal number system implement all possible combinations of octal digits in the binary number system: from 0 (000) to 7 (111). To convert a binary number to octal, you need to combine the binary digits into groups of 3 digits (triads) in two directions, starting from the separator of the integer and fractional part. If necessary, insignificant zeros should be added to the left of the original number. If the number contains a fractional part, then insignificant zeros can also be added to the right of it until all triads are filled. Then each triad is replaced by an octal digit.

Example: Convert the number 1101110.012 into the octal number system.

Combine binary digits into triads from right to left. We get

To convert a number from the octal system to binary, you need to write each octal digit with its binary code:

Four digits of the hexadecimal number system implement all possible combinations of hexadecimal digits in the binary number system: from 0 (0000) to F (1111). To convert a binary number to hexadecimal, you need to combine binary digits into groups of 4 digits (tetrads) in two directions, starting from the separator of the integer and fractional part. If necessary, insignificant zeros should be added to the left of the original number. If the number contains a fractional part, then to the right of it, you also need to add insignificant zeros to fill all the tetrads. Then each tetrad is replaced by a hexadecimal digit.

Example: Convert the number 1101110.112 to hexadecimal notation.

Combine binary numbers into notebooks from right to left. We get

To translate a number from the hexadecimal system to binary, you need to write each hexadecimal digit with a binary code:

## Integer conversion

To transfer, you need to divide the original number on the basis of the new number system until you get the whole remainder, which is the least significant digit in the new number system (unit). The resulting quotient is again divided by the base of the system, and so on, until the quotient becomes less than the base of the new number system. All operations are performed in the original number system.

Consider, for example, the conversion of a number from a decimal number system to a binary number system.

Take a decimal number and divide it by the base of the binary system, that is, number 2. We will divide by a corner: As a result of the first division, we obtain the discharge of units (the least significant discharge). As a result of the second division, we obtain the category of twos. We continue the division until the result of division is more than two. At the end of the conversion operation, we got the binary number 11111002.

Now we will translate the same number into the octal number system. For this, the number 12410 divide by the number 8:

As we can see, the remainder of the first division is 4. That is, the least significant digit of the octal number contains 4. The remainder of the second division is 7. that is, the second digit of the octal number is 7. The highest digit is 1. That is, as a result of multiple divisions we got the octal number 1748.

Check if we were mistaken in the conversion process? To do this, we transform the resulting binary number into a decimal system according to the usual decomposition formula:

, Is it possible to translate from the octal number system to binary division? Can! But the division must be done according to the rules of octal arithmetic. The rules for working in the octal number system will be discussed in the next chapter. Nevertheless, for completeness, consider an example of binary conversion of the previously obtained octal number 1748. Divide it on the basis of the new number system 2. As we were convinced to carry out division in the octal system it is very inconvenient, because subconsciously we divide in the decimal number system. Let's pay attention to the fact that the number 8 is a power of 2. That is, you can think of the octal number system as just a shorter notation for a binary number. This means that you can use three binary bits (8 = 2 3) to represent an octal digit. Let's make a correspondence table. It is shown in table 1.

Table 1. Octal and binary code mapping table

 Binary code Octal digit Decimal equivalent 000 0 0 001 1 1 010 2 2 011 3 3 100 4 4 101 5 5 110 6 6 111 7 7

Using this table, you can simply replace each octal digit with three binary bits. Three binary bits are commonly called a triad or tribit. Now let's convert the octal number 1748 to binary using table 7:

Similarly, you can convert a number from binary to octal. To do this, the binary number is divided into triads relative to the rightmost digit (or binary comma) and, using table 7, each triad is assigned an octal digit.

Similarly, you can translate a number from hexadecimal to binary and vice versa. In this case, four binary digits are required to represent the hexadecimal digit. Four binary digits are usually called a tetrad. Sometimes when translating foreign books the term nibl is used.

Let's make a table of correspondence between binary tetrads and hexadecimal digits. To do this, we will simply add one to the value of the previous row in each column of the table, in accordance with the number system used in this column. The result is shown in table 2.

As an example of using table 2, we translate the hexadecimal number 7C16 in binary form:

Table 2. Hexadecimal and Binary Code Conversion Chart

 Binary code Octal digit Decimal equivalent 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 a a 1011 b b 1100 c c 1101 d d 1110 e e 1111 f f

An example of converting a binary number to octal and hexadecimal is shown in Figure 1.

Figure 1. An example of converting a binary number to octal and hexadecimal.

In the figure below, binary notebooks and their corresponding hexadecimal digits are highlighted. Their correspondence can be checked using table 2. The triads and the corresponding octal digits are highlighted above. The eldest triad was incomplete. It needs to be supplemented with leading insignificant zeros in order to be able to use table 1.

Together with the article "Converting numbers from one number system to another," they read:

## Number conversion table

Decimal SSBinary ssQuaternary SSOctal SSHex SS
11111
210222
311333
41001044
51011155
61101266
71111377
8100020108
9100121119
1010102212A
1110112313B
1211003014C
1311013115D
1411103216E
1511113317F
16100001002010

## How to translate a number from a binary number system

To translate a number from a binary number system to a quaternary, octal or hexadecimal system, you need to use the translation algorithm:

1. Divide the binary number from right to left into groups of 2 (for quaternary SS), 3 (for octal SS) or 4 (for hexadecimal SS) digits. If the numbers for the full group are not enough on the left, you need to add the required number of insignificant zeros.
2. Replace each group of numbers with its analogue in the corresponding number system.

Convert number 1111001102 from binary to quaternary.

We break the number into groups of 2 digits from right to left and replace each group with an analog in the quaternary number system from the table:

1111001102 = 01 11 10 01 10 = 132124

Convert number 1111001102 from binary to octal.

We break the number into groups of 3 digits from right to left and replace each group with an analog in the octal number system from the table:

1111001102 = 111 100 110 = 7468

Convert number 1111001102 from binary to hexadecimal.

We break the number into groups of 4 digits from right to left and replace each group with an analog in the hexadecimal notation from the table:

1111001102 = 0001 1110 0110 = 1E616

## How to convert a number to binary

To translate a number from a quaternary, octal or hexadecimal number system to binary, you need to use the translation algorithm:

1. Replace each digit with a binary analog consisting of 2 (for quaternary), 3 (for octal) or 4 (for hexadecimal) digits. If necessary, the number is padded with zeros on the left.
2. Cross out insignificant zeros.

Convert number 1203234 from quaternary to binary.

We replace each digit with a group of 2 digits in the binary number system:

1203234 = 01 10 00 11 10 11 = 110001110112

Convert number 264750308 from octal to binary.

We replace each digit with a group of 3 digits in the binary number system:

264750308 = 010 110 100 111 101 000 011 000 = 101101001111010000110002

Convert number 2AC0F7416 from hexadecimal to binary.

We replace each digit with a group of 4 digits in the binary number system:

2AC0F7416 = 0010 1010 1100 0000 1111 0111 0100 = 101010110000001111011101002

Share the article with classmates "How to convert from binary to octal, hexadecimal and quaternary systems».

## Algorithm for translating numbers from one number system to another

Example No. 1.  Translation from 2 to 8 to 16 number system.
These systems are multiples of two, therefore, the translation is carried out using the correspondence table (see below).

To convert a number from the binary number system to octal (hexadecimal), it is necessary to divide the binary number to the right and left from the comma into groups of three (four for hexadecimal) digits, padding the outermost groups if necessary with zeros. Each group is replaced with the corresponding octal or hexadecimal digit.

Example No. 2. 1010111010,1011 = 1.010.111.010,101.1 = 1272.518
here 001 = 1, 010 = 2, 111 = 7, 010 = 2, 101 = 5, 001 = 1

When translating to the hexadecimal system, it is necessary to divide the number into parts, four digits, observing the same rules.
Example No. 3. 1010111010,1011 = 10.1011.1010,1011 = 2B12,13Hex
here 0010 = 2, 1011 = B, 1010 = 12, 1011 = 13

The translation of numbers from 2, 8 and 16 into the decimal system of calculus is done by breaking the number into separate ones and multiplying it by the base of the system (from which the number is converted) raised to the power corresponding to its ordinal number in the translated number. In this case, the numbers are numbered to the left of the comma (the first number is number 0) with increasing, and to the right side with decreasing (i.e. with a negative sign). The results obtained are added up.

Example No. 4.
Example of conversion from binary to decimal number system. An example of a conversion from octal to decimal number system. An example of a conversion from hexadecimal to decimal.

Pin
Send
Share
Send
Send