• Hexadecimal code. Hexadecimal and binary number systems

    Many computer users understand that a computer operates in a binary number system. Traditionally, the states of a binary system are represented by the numbers 0 and 1, although, more precisely, each state indicates the presence or absence of a signal, i.e. it would be more correct to call the states “off” and “on”, or “no” and “yes”. The state “off” or “no” corresponds to the number 0, and the state “on” or “yes” corresponds to the number 1. For ordinary users Usually there is no need to fully understand the structure of the computer, but the binary number system makes itself felt in the form of various restrictions based on powers of two. A more compact version of the binary system is called hexadecimal. The number sixteen is the fourth power of two. It follows from this that you can quite simply convert long binary sequences of zeros and ones into short hexadecimal ones. To do this, simply break the binary sequence into groups of four digits (digits) starting with the least significant digit (on the right) and replace each group with the corresponding hexadecimal value.

    The hexadecimal system is usually used for the convenience of perceiving binary data, since conversions from the hexadecimal system to the binary system and back are carried out by simply replacing strings. The computer works exclusively with binary sequences, and the hexadecimal notation of this sequence is four times more compact, since this system has a base of 16 (2 16), and binary is 2. The binary sequence can be quite cumbersome. For example, writing the number 513 requires ten binary digits (1000000001), but only three in hexadecimal (201). However, sixteen is required to represent any hexadecimal numbers different characters, and not ten, which are used in the decimal number system familiar to us. The first ten characters are characters in the range from 0 to 9, the rest are letters of the Latin alphabet in the range from A to F. The letters are usually (but not always) written in uppercase (capital) in hexadecimal notation of the number. The first ten characters (from 0 to 9) are written similarly to numbers in the decimal number system and correspond to them. Letters in the range A through F correspond to values ​​in the range 10 to 15.

    Let's consider the correspondence of numbers from 0 to 15 in hexadecimal and binary number systems.

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

    The entries for 10, 11, etc. in decimal, binary, and hexadecimal systems do not correspond to each other. Let's look at a small example. Let us have hexadecimal number number 1A5E. for transfer to binary notation Simply replace the hexadecimal digits with the corresponding binary groups. The result is 0001 1010 0101 1110. If we remove the insignificant zeros in front of the number and write it without separators, we get 1101001011110. For the reverse translation, we divide the number into groups of four digits, starting with the least significant (on the right side), and also for convenience we add insignificant zeros to senior group up to 4 digits. We get 0001 1010 0101 1110. Replace the groups with the corresponding ones hexadecimal values, we get 1A5E.

    To convert a hexadecimal number into a decimal representation, you can use the scheme by which we write decimal numbers. In a decimal number, each digit represents the corresponding power of ten, starting from zero and increasing from right to left. For example, decimal number 123 means 1*10 2 + 2*10 1 + 3*10 0 . Using a similar method, we convert the number 1A5E to the decimal number system. In the hexadecimal number system, as well as in the decimal number system, each digit denotes the corresponding power of the number sixteen, starting from zero and increasing from right to left. The characters 1 and 5 in hexadecimal correspond to the values ​​1 and 5 in decimal, and the characters A and E correspond to 10 and 14. Then 1A5E can be represented in decimal as 1*16 3 + 10*16 2 + 5*16 1 + 14*16 0 = 6750. However, to evaluate hexadecimal numbers it is not at all necessary to convert them to decimal. The rules for comparison, addition and multiplication in this system are the same as in the decimal system, the main thing is not to forget that each digit can contain values ​​from 0 to 15. For more quick translation number between the number system can be used standard calculator in Windows, to do this, just select the number system in the advanced mode of the calculator, enter a number in it and select the right system number in which the result should be displayed.

    Because numeric-only hexadecimal numbers are easily confused with decimal numbers, they are usually marked in a way that makes it clear that hexadecimal notation is used. Hexadecimal entries are usually marked by either adding a lowercase "h" to the end or adding a "0x" prefix before the number. Thus, the hexadecimal number 1A5E can be written as 1A5Eh or 0x1A5E, where a trailing “h” or a leading “0x” indicates that hexadecimal notation is used.

    Originated in ancient Babylon. In India, the system works in the form of positional decimal numbering using zero, among Hindus this system numbers were borrowed by the Arab nation, and the Europeans, in turn, took them from them. In Europe, this system began to be called Arabic.

    Positional systemdead reckoning— the meaning of all digits depends on the position (digit) of the given digit in the number.

    Examples, the standard decimal number system is a positional system. Let's say given a number453 . Number 4 stands for hundreds and corresponds to a number400, 5 - number of tens and corresponds to the value50 , A 3 - units and meaning3 . It is easy to see that as the digit increases, the value increases. Thus, we write the given number as a sum400+50+3=453.

    Hexadecimal number system.

    Hexadecimal number system(hexadecimal numbers) - positional number system. Hexadecimal base is the number 16.

    Writing numbers in octal number system we get quite compact expressions, but in hexadecimal we get more compact expressions.

    The first ten digits of the sixteen hexadecimal digits are standard spacing 0 - 9 , the next six digits are expressed using the first letters of the Latin alphabet: A, B, C, D, E, F. Convert from hexadecimal to binary and reverse side do the same process for octal system.

    Application of the hexadecimal number system.

    The hexadecimal number system is used quite well in modern computers, For example use it to indicate color: #FFFFFF- white color.

    Converting numbers from one number system to another.

    Converting numbers from hexadecimal to decimal.

    To convert a hexadecimal number to decimal, you need to reduce the given number to the form of the sum of the products of the powers of the base of the hexadecimal number system by the corresponding digits in the digits of the hexadecimal number.

    For example, convert the hexadecimal number 5A3 to decimal. Here 3 numbers. Based on the above rule, we reduce it to the form of a sum of powers with a base of 16:

    5A3 16 = 3·16 0 +10·16 1 +5·16 2 = 3·1+10·16+5·256 = 3+160+1280 = 1443 10

    Converting numbers from binary to hexadecimal and vice versa.

    To translate polysemantic binary number in the hexadecimal system, you need to divide it into tetrads from right to left and change all tetrads with the corresponding hexadecimal digit. To convert a number from the hexadecimal system to the binary system, you need to change each digit to the corresponding tetrads from the conversion table, which you will find below.

    For example:

    010110100011 2 = 0101 1010 0011 = 5A3 16

    Number conversion table.

    An algorithm for converting numbers from one number system to another.

    1. From decimal system notation:

    • divide the number by the base of the translated number system;
    • find the remainder when dividing the integer part of a number;
    • write down all remainders from division in reverse order;

    2. From the binary number system:

    • to convert to the decimal number system, we find the sum of the products of base 2 by the corresponding degree of digit;
    • To convert a number to octal, we divide the number into triads.

    For example, 1000110 = 1,000,110 = 1068

    • To convert a number from the binary number system to hexadecimal, we divide the number into groups of 4 digits.

    For example, 1000110 = 100 0110 = 4616.

    Translation tables:

    Binary SS

    Hexadecimal SS

    0000

    0001

    0010

    0011

    0100

    0101

    0110

    0111

    1000

    1001

    1010

    1011

    1100

    1101

    1110

    1111

    Binary SS

    The result has already been received!

    Number systems

    There are positional and non-positional number systems. The Arabic number system, which we use in everyday life, is positional, but the Roman number system is not. IN positional systems In notation, the position of a number uniquely determines the size of the number. Let's consider this using the example of the number 6372 in the decimal number system. Let's number this number from right to left starting from zero:

    Then the number 6372 can be represented as follows:

    6372=6000+300+70+2 =6·10 3 +3·10 2 +7·10 1 +2·10 0 .

    The number 10 defines the number system (in in this case this is 10). The values ​​of the position of a given number are taken as powers.

    Consider the real decimal number 1287.923. Let's number it starting from zero, positioning the number from the decimal point to the left and right:

    Then the number 1287.923 can be represented as:

    1287.923 =1000+200+80 +7+0.9+0.02+0.003 = 1·10 3 +2·10 2 +8·10 1 +7·10 0 +9·10 -1 +2·10 -2 +3· 10 -3.

    In general, the formula can be represented as follows:

    C n s n +C n-1 · s n-1 +...+C 1 · s 1 +C 0 ·s 0 +D -1 ·s -1 +D -2 ·s -2 +...+D -k ·s -k

    where C n is an integer in position n, D -k - fractional number in position (-k), s- number system.

    A few words about number systems. A number in the decimal number system consists of many digits (0,1,2,3,4,5,6,7,8,9), in the octal number system it consists of many digits (0,1, 2,3,4,5,6,7), in the binary number system - from a set of digits (0,1), in the hexadecimal number system - from a set of digits (0,1,2,3,4,5,6, 7,8,9,A,B,C,D,E,F), where A,B,C,D,E,F correspond to the numbers 10,11,12,13,14,15. In the table Tab.1 numbers are presented in different systems Reckoning.

    Table 1
    Notation
    10 2 8 16
    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

    Converting numbers from one number system to another

    To convert numbers from one number system to another, the easiest way is to first convert the number to the decimal number system, and then convert from the decimal number system to the required number system.

    Converting numbers from any number system to the decimal number system

    Using formula (1), you can convert numbers from any number system to the decimal number system.

    Example 1. Convert the number 1011101.001 from binary number system (SS) to decimal SS. Solution:

    1 ·2 6 +0 ·2 5 + 1 ·2 4 + 1 ·2 3 + 1 ·2 2 + 0 ·2 1 + 1 ·2 0 + 0 ·2 -1 + 0 ·2 -2 + 1 ·2 -3 =64+16+8+4+1+1/8=93.125

    Example2. Convert the number 1011101.001 from octal number system (SS) to decimal SS. Solution:

    Example 3 . Convert the number AB572.CDF from hexadecimal number system to decimal SS. Solution:

    Here A-replaced by 10, B- at 11, C- at 12, F- by 15.

    Converting numbers from the decimal number system to another number system

    To convert numbers from the decimal number system to another number system, you need to convert the integer part of the number and the fractional part of the number separately.

    The integer part of a number is converted from decimal SS to another number system by sequentially dividing the integer part of the number by the base of the number system (for binary SS - by 2, for 8-ary SS - by 8, for 16-ary SS - by 16, etc. ) until a whole residue is obtained, less than the base CC.

    Example 4 . Let's convert the number 159 from decimal SS to binary SS:

    159 2
    158 79 2
    1 78 39 2
    1 38 19 2
    1 18 9 2
    1 8 4 2
    1 4 2 2
    0 2 1
    0

    As can be seen from Fig. 1, the number 159 when divided by 2 gives the quotient 79 and remainder 1. Further, the number 79 when divided by 2 gives the quotient 39 and remainder 1, etc. As a result, constructing a number from division remainders (from right to left), we obtain a number in binary SS: 10011111 . Therefore we can write:

    159 10 =10011111 2 .

    Example 5 . Let's convert the number 615 from decimal SS to octal SS.

    615 8
    608 76 8
    7 72 9 8
    4 8 1
    1

    When converting a number from a decimal SS to an octal SS, you need to sequentially divide the number by 8 until you get an integer remainder less than 8. As a result, constructing a number from division remainders (from right to left) we get a number in octal SS: 1147 (see Fig. 2). Therefore we can write:

    615 10 =1147 8 .

    Example 6 . Let's convert the number 19673 from the decimal number system to hexadecimal SS.

    19673 16
    19664 1229 16
    9 1216 76 16
    13 64 4
    12

    As can be seen from Figure 3, by successively dividing the number 19673 by 16, the remainders are 4, 12, 13, 9. In the hexadecimal number system, the number 12 corresponds to C, the number 13 to D. Therefore, our hexadecimal number is 4CD9.

    To convert proper decimal fractions ( real number with a zero integer part) into a number system with base s, it is necessary to sequentially multiply this number by s until the fractional part is pure zero, or we obtain the required number of digits. If, during multiplication, a number with an integer part other than zero is obtained, then this integer part is not taken into account (they are sequentially included in the result).

    Let's look at the above with examples.

    Example 7 . Let's convert the number 0.214 from the decimal number system to binary SS.

    0.214
    x 2
    0 0.428
    x 2
    0 0.856
    x 2
    1 0.712
    x 2
    1 0.424
    x 2
    0 0.848
    x 2
    1 0.696
    x 2
    1 0.392

    As can be seen from Fig. 4, the number 0.214 is sequentially multiplied by 2. If the result of multiplication is a number with an integer part other than zero, then the integer part is written separately (to the left of the number), and the number is written with a zero integer part. If the multiplication results in a number with a zero integer part, then a zero is written to the left of it. The multiplication process continues until the fractional part reaches a pure zero or we obtain the required number of digits. Writing bold numbers (Fig. 4) from top to bottom we get the required number in the binary number system: 0. 0011011 .

    Therefore we can write:

    0.214 10 =0.0011011 2 .

    Example 8 . Let's convert the number 0.125 from the decimal number system to binary SS.

    0.125
    x 2
    0 0.25
    x 2
    0 0.5
    x 2
    1 0.0

    To convert the number 0.125 from decimal SS to binary, this number is sequentially multiplied by 2. In the third stage, the result is 0. Consequently, the following result is obtained:

    0.125 10 =0.001 2 .

    Example 9 . Let's convert the number 0.214 from the decimal number system to hexadecimal SS.

    0.214
    x 16
    3 0.424
    x 16
    6 0.784
    x 16
    12 0.544
    x 16
    8 0.704
    x 16
    11 0.264
    x 16
    4 0.224

    Following examples 4 and 5, we get the numbers 3, 6, 12, 8, 11, 4. But in hexadecimal SS, the numbers 12 and 11 correspond to the numbers C and B. Therefore, we have:

    0.214 10 =0.36C8B4 16 .

    Example 10 . Let's convert the number 0.512 from the decimal number system to octal SS.

    0.512
    x 8
    4 0.096
    x 8
    0 0.768
    x 8
    6 0.144
    x 8
    1 0.152
    x 8
    1 0.216
    x 8
    1 0.728

    Received:

    0.512 10 =0.406111 8 .

    Example 11 . Let's convert the number 159.125 from the decimal number system to binary SS. To do this, we translate separately the integer part of the number (Example 4) and the fractional part of the number (Example 8). Further combining these results we get:

    159.125 10 =10011111.001 2 .

    Example 12 . Let's convert the number 19673.214 from the decimal number system to hexadecimal SS. To do this, we translate separately the integer part of the number (Example 6) and the fractional part of the number (Example 9). Further, combining these results we obtain.