Hexadecimal number system. How to convert a decimal number to a hexadecimal number
Hexadecimal number system, is by far the most popular means of compactly recording binary numbers. Very widely used in the development and design of digital technology.
As the name suggests, the base of this system is the number sixteen 16 or in hexadecimal 10 16. To avoid confusion, when writing numbers in number systems other than decimal, we will indicate the base of the number system at the bottom right of the main number record. Since the base of the system is the number sixteen, it means that to represent the numbers we need sixteen digits. The first ten digits are taken from the decimal system familiar to us (0,1,..,8,9) and six letters of the Latin alphabet (a,b,c,d,e,f) are also added. For example, in the hexadecimal number 3f7c2, the letters "f" and "c" are hexadecimal digits.
Counting in hexadecimal is similar to counting in decimal. Let's try to count and write numbers by constructing them from the available sixteen digits:
Zero - 0
;
One - 1
;
Two - 2
;
...
and so on…
...
Eight - 8
;
Nine - 9
;
Ten - a;
Eleven - b;
Twelve - c;
Thirteen - d;
Fourteen - e;
Fifteen - f;
What to do next? All the numbers are gone. How to depict the number Sixteen? Let's do the same as we did in the decimal system. There we introduced the concept of ten, here we will introduce the concept of “sixteen” and say that sixteen is one “sixteen” and zero ones. And this can already be written down - “10 16”.
So, Sixteen - 10
16 (one "sixteen", zero ones)
Seventeen - 11
16 (one "sixteen", one unit)
...
and so on…
...
Twenty five - 19
16 (one "sixteen", nine ones)
Twenty six - 1a 16 (one "sixteen", ten ones)
Twenty seven - 1b 16 (one "sixteen", eleven ones)
...
and so on…
...
Thirty - 1e 16 (one "sixteen", fourteen ones)
Thirty one - 1f 16 (one "sixteen", fifteen ones)
Thirty two - 20
16 (two sixteens, zero ones)
Thirty three - 21
16 (two sixteens, one one)
...
and so on…
...
two hundred fifty five - ff 16 (fifteen by "sixteen", fifteen units)
two hundred fifty six - 100
16 (one "two hundred fifty-six", zero "sixteen", zero ones)
two hundred fifty seven - 101
16 (one "two hundred fifty-six", zero through "sixteen", one one)
two hundred fifty eight - 102
16 (one "Two hundred and fifty six", zero through "sixteen", two ones)
...
and so on...
...
Whenever we have exhausted the set of digits to display the next number, we enter larger units of counting (i.e. counting by “sixteen”, “two hundred and fifty-six”, etc.) and write the number with an extension of one digit .
Consider the number 3e2c 16 written in hexadecimal number system. About it we can say that it contains: three x four thousand ninety-six, “e” (fourteen) x two hundred fifty-six, two x sixteen and “c” (twelve) ones. And you can get its value through the numbers included in it as follows.
3e2c 16 = 3 *4096+14 *256+2 *16+12 *1, here and below the * (asterisk) sign means multiplication.
But the series of numbers 4096, 256, 16, 1 is nothing more than integer powers of the number sixteen (the base of the number system) and therefore can be written:
3e2c 16 = 3 *16 3 +14 *16 2 +2 *16 1 +12 *16 0
Similarly for a hexadecimal fraction (fractional number) for example: 0.5a2 16 about it we can say that it contains: five sixteenths, “a” (ten) two hundred and fifty-sixths and two four thousand and ninety-sixths. And its value can be calculated as follows:
0.5a2 16 = 5 *(1/16) + 10 *(1/256) + 2 *(1/4096)
And here is a series of numbers 1/16; 1/256 and 1/4096 are nothing more than integer powers of sixteen and we can also write:
0.5a2 16 = 5 *16 -1 + 10 *16 -2 + 2 *16 -3
For the mixed number 7b2.1f9 we can write in the same way:
7b2.1f9 = 7 *16 2 +11 *16 1 +2 *16 0 +1 *16 -1 +15 *16 -2 +9 *16 -3
Let's number the digits of the integer part of some hexadecimal number, from right to left, as 0,1,2...n (numbering starts from zero!). And the digits of the fractional part, from left to right, like -1,-2,-3...-m, then the value of a certain hexadecimal number can be calculated using the formula:
N = d n 16 n +d n-1 16 n-1 +…+d 1 16 1 +d 0 16 0 +d -1 16 -1 +d -2 16 -2 +…+d -(m-1) 16 -(m-1) +d -m 16 -m
Where: n- the number of digits in the integer part of the number minus one;
m- the number of digits in the fractional part of the number
d i- digit standing in i-th rank
This formula is called the formula for the bitwise expansion of a hexadecimal number, i.e. number written in hexadecimal number system. If we replace the number sixteen in this formula with some arbitrary number q, then we obtain the expansion formula for the number written in qth number system, i.e. with base q:
N = d n q n +d n-1 q n-1 +…+d 1 q 1 +d 0 q 0 +d -1 q -1 +d -2 q -2 +…+d -(m-1) q - (m-1) +d -m q -m
Using this formula you can always calculate the value of a number written in any positional number system with a base q.
Other number systems can be found on our website using the following links.
The hexadecimal number system has an alphabet consisting of 16 digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, b, c, d, e, f.
When writing a number in the hexadecimal system, the letters A, B, C, D, E, F are used respectively to write the digits denoting the numbers 10, 11, 12. 13, 14. 15.
Converting numbers from hexadecimal to decimal
You can convert any hexadecimal number to decimal using the already known formula
Examples.
AE07 16 =10∙16 3 +14∙16 2 +0∙16 1 +7∙16 0 =44551 10 .
100 16 =1∙16 2 +0∙16 1 +0∙16 0 =256 10 .
58 16 =5∙16 1 +8∙16 0 =.88 10 .
2A 16 =2∙16 1 +10∙16 0 =42 10.
Converting a number from the decimal system to hexadecimal is carried out in the same way as to binary.
Converting numbers from hexadecimal to binary and vice versa
You can convert any hexadecimal number to binary as follows. Each digit of a hexadecimal number is written as a four-digit binary number - notebook. After this, the zeros on the left can be discarded.
2) 2A= 0010 1010 2 = 101010 2 . |
3) 58 16 = 0101 1000 2 = 1011000 2 . |
Conversely, you can convert any binary number to hexadecimal in the same way. Every four binary digits, counting from right to left, are written as one hexadecimal digit. These numbers are also located from right to left.
Examples.
2. 101010 2 = 10 1010 2 = 2A.
3. 1011000 2 = 101 1000 2 = 58 16 .
Octal number system
The octal number system has an alphabet consisting of 8 digits:
0, 1, 2, 3, 4, 5, 6, 7.
Converting a number from the decimal system to octal and back is carried out in the same way as converting to/from binary.
Converting numbers from octal to binary and back
Each digit of an octal number is written as a three-digit binary number - triad.
Examples.
2563 8 = 010 101 110 011 2 =10101110011 2 .
1001101 2 = 001 001 101 2 = 115 8 .
Methodological materials for laboratory lesson No. 1
Topic of the laboratory lesson: Number systems. Measuring information.
Number of hours: 2.
Examples with solutions
Translation fromp -ary system to 10-ary system. Suppose we need to convert a number in a certain number system to decimal. To do this, you need to represent it in the form
11100110 2 = 1∙2 7 + 1∙2 6 + 1∙2 5 + 0∙2 4 + 0∙2 3 + 1∙2 2 + 1∙2 1 + 0∙2 0 = 128 + 64 + 32 + 4 + 2 = 230 10 .
2401 5 = 2∙5 3 + 4∙5 2 + 0∙5 1 + 1∙5 0 = 250 + 100 + 0 + 1 = 351.
Conversion from 10-digit system top -ichnaya.
2.1 98 10 → X 2.
Divide the number by 2. Then divide the incomplete quotient by 2. We continue until the incomplete quotient becomes less than 2, i.e. equal to 1.
98: 2 = 49. Remainder - 0 .
49: 2 = 24. Remainder - 1 .
24: 2 = 12. Remainder - 0 .
12: 2 = 6. Remainder - 0 .
6: 2 = 3. Remainder - 0 .
3: 2 = 1 . Remainder - 1 .
Since the last partial quotient is 1, the process is over. We write down all the remainders from bottom to top, starting with the last incomplete quotient, and we get the number 1100010. So 98 10 = 1100010 2.
2.2 2391 10 → X 16.
Divide the number by 16. Then divide the partial quotient by 16. Continue until the partial quotient is less than 16.
2391: 16 = 149. Remainder - 7 .
149: 16 = 9 . Remainder - 5 .
Since the last partial quotient (9) is less than 16, the process is over. We write down, starting from the last incomplete quotient, all the remainders from bottom to top and we get the number 957. So 2391 10 = 957 16.
2.3 12165 10 → X 2.
If you convert by division to the binary system, you get a rather cumbersome process. You can first convert the number to octal, and then replace the octal digits from right to left with triads.
12165 10 = 27605 8 = 010 111 110 000 101 = 10111110000101.
Determining the base of a number systemp .
One boy wrote about himself: “I have 24 fingers, 5 on each hand, and 12 on my feet.” How can this be?
Solution. It is necessary to determine the base of the number system p. Since we know that there are only 10 toes 10, then 12 p =1∙p+2 = 10 10 . From here we get the equation p + 2 = 10 p= 8. So the boy meant numbers in the octal system. Indeed, there are 24 8 = 2∙8+4 = 20 10 toes, and 12 8 = 1∙8+2 = 10 10 toes.
The result has already been received!
Number systems
There are positional and non-positional number systems. The Arabic number system that we use in everyday life, is positional, but Roman is not. In positional number systems, the position of a number uniquely determines the magnitude 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 determines the number system (in this case it 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, position of 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 decimal system 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 separately convert the whole part of the number and fractional part numbers.
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 decimal SS to 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 regular decimal fractions (a real number with a zero integer part) into a number system with base s, it is necessary to successively multiply this number by s until the fractional part contains a pure zero, or we obtain the required number of digits. If the multiplication results in a number with an integer part other than zero, 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.
The number system familiar to humans is decimal. It is based on ten digits from 0 to 9. The hexadecimal system is distinguished by the presence in it of the first six letters of the Latin alphabet for writing numbers in addition to the basic numbers. That is, the number 9 is followed by the symbol “A”, which corresponds to the number 10 for the decimal system. Accordingly, F in hexadecimal is 16 in decimal. The use of sixteen characters in the system is not a random choice.
The unit of information is a bit. Eight bits form a byte. There is a concept like a machine word - a unit of data representing two, that is, sixteen bits. Thus, using sixteen different symbols, it is possible to describe any information that will be the smallest particle during data exchange. You can perform any arithmetic operations with them, and the result, accordingly, will also be obtained in the hexadecimal system.
In order to distinguish that a number is written in hexadecimal, the letter “h” or the subscript “16” is written after it.
Application
The most widespread use of the hexadecimal number system is in error codes of software products, for example, operating system. The numbers contained in these codes are standardized. Having a special table, you can always determine what exactly this or that error means.
In languages low level, as close as possible to machine codes, the hexadecimal system is used to write programs. Many programmers also use it when working with languages. high level, because the numbers in this system, using a special correspondence table, are easily converted into the binary system on which the operation of all digital technology is based. Any information on the computer, be it a music file or text document, after translation, is represented by a sequence of the original binary code, and it is more convenient to view it represented by hexadecimal symbols.
Also, one of the uses of hexadecimal characters is the description of color schemes, that is, the three components R, G, B are described in a manner appropriate to a given system. This recording approach is called hexadecimal color
The ability to view a program in hexadecimal code allows you to debug it, make changes, and attackers use this approach to hack programs.
Hexadecimal number system(also known as hexadecimal code) is a positional number system with an integer base of 16. The term hex (pronounced hex, short for English hexadecimal) is also sometimes used in the literature. The digits of this number system are usually used in Arabic numerals 0-9, as well as the first characters of the Latin alphabet A-F. The letters correspond to the following decimal values:
- * A -10;
- *B—11;
- *C—12;
- * D -13;
- * E - 14;
- * F - 15.
Thus, ten Arabic numerals, coupled with six Latin letters, make up the sixteen digits of the system.
By the way, on our website you can convert any text into decimal, hexadecimal, binary code using the Online Code Calculator.
Application. Hex code widely used in low-level programming as well as in various computer reference documents. The popularity of the system is justified by architectural solutions modern computers: They have a byte (consisting of eight bits) as the minimum unit of information - and the value of a byte is conveniently written using two hexadecimal digits. The byte value can range from #00 to #FF (0 to 255 in decimal notation) - in other words, using hexadecimal code, you can write any state of the byte, while there are no “extra” digits not used in the recording.
Encoded Unicode Four hexadecimal digits are used to record the character number. The RGB color notation (Red, Green, Blue) also often uses hexadecimal code (for example, #FF0000 is a bright red color notation).
A method for writing hexadecimal code.
Mathematical way of writing. In mathematical notation, the base of the system is written in decimal form as a subscript to the right of the number. The decimal notation of the number 3032 can be written as 3032 10, in the hexadecimal system this number will have the notation BD8 16.
In the syntax of programming languages. The syntax of different programming languages sets differently the format for writing a number using hexadecimal code:
* The syntax of some varieties of assembly language uses the Latin letter “h”, which is placed to the right of the number, for example: 20Dh. If a number begins with a Latin letter, then a zero is placed in front of it, for example: 0A0Bh. This is done in order to distinguish values using constants from constants. hexadecimal code;
* Other types of assembler, as well as Pascal (and its variants such as Delphi) and some Basic dialects, use the "$" prefix: $A15;
* In language HTML markup, as well as in cascade CSS files, to indicate a color in RGB format with a hexadecimal notation, the prefix “#” is used: #00DC00.
How to convert hexadecimal code to another system?
Convert from hexadecimal to decimal. To perform a conversion operation from the hexadecimal system to the decimal system, you need to represent the original number as the sum of the products of the digits in the digits of the hexadecimal number and the power of the base.
Binary SS |
hex SS |
For example, you need to translate the hexadecimal number A14: it has three digits. Using the rule, we write it as a sum of powers with a base of 16:
A14 16 = 10.16 2 + 1.16 1 + 4.16 0 = 10.256 + 1.16 + 4.1 = 2560 + 16 + 4 = 2580 10
Converting numbers from binary to hexadecimal and vice versa.
A notebook table is used for translation. To convert a number from the binary to the decimal system, you need to split it into separate tetrads from right to left, and then, using the table, replace each tetrad with the corresponding hexadecimal digit. Moreover, if the number of digits is not a multiple of four, then it is necessary to add the corresponding number of zeros to the right of the number so that the total number of binary digits becomes a multiple of four.
Table of notebooks for translation.
To convert from hexadecimal to binary, you need to perform the reverse operation: replace each digit with a tetrad from the table.
Binary SS |
Octal SS |
Example conversion from hexadecimal to binary: A5E 16 = 1010 0101 1110 = 101001011110 2
Example conversion from binary to hexadecimal: 111100111 2 = 0001 1110 0111 = 1E7 16
In this example, the number of digits in the original binary number was not four (9), so leading zeros were added for a total number of digits of 12.
Automatic translation. Quick conversion from hexadecimal number system to one of three popular systems(binary, octal and decimal), as well as reverse translation, can be performed using a standard calculator included with Windows OS. Open the calculator, select View -> Programmer from the menu. In this mode, you can set the number system used in at the moment(see menu on the left: Hex, Dec, Oct, Bin). In this case, changing the current number system automatically produces a translation.