Illustrated tutorial on digital graphics. Converting numbers from one number system to another online

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. Ordinary users usually do not need to fully understand the structure of the computer, however, 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 split 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 base 16 (2 16), and binary 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, to represent any hexadecimal number, sixteen different symbols are required, rather than the ten that are used in the decimal number system we are familiar with. 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 a hexadecimal number 1A5E. To convert to binary, simply replace the hexadecimal digits with the corresponding binary groups. The result will be 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 lowest (on the right side), and also for convenience we add insignificant zeros in the highest group to 4 ranks. We get 0001 1010 0101 1110. Replace the groups with the corresponding 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, the 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. To quickly convert numbers between number systems, you can use a standard calculator in Windows, for this it is enough to In the advanced mode of the calculator, select a number system, enter a number in it and select the desired number system in which to display the result.

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.

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 the architectural solutions of 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 the HTML markup language, as well as in cascading CSS files, the prefix “#” is used to indicate a color in RGB format with a hexadecimal notation: #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. A quick conversion from the hexadecimal number system to one of the three popular systems (binary, octal and decimal), as well as the reverse conversion, 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 currently in use (see menu on the left: Hex, Dec, Oct, Bin). In this case, changing the current number system automatically produces a translation.

To write programs in Assembly, you need to understand the hexadecimal number system. There is nothing complicated about it. We use the decimal system in life. I'm sure you all know it, so I'll try to explain the hexadecimal system using an analogy with the decimal system.

So, in the decimal system, if we add a zero to any number on the right, then this number will increase by 10 times. For example: 1 x 10 = 10; 10 x 10 = 100; 100 x 10 = 1000, etc. In this system we use numbers from 0 to 9, i.e. ten different numbers (in fact, that’s why it’s called decimal).

In the hexadecimal system, we use sixteen "digits". I specifically wrote the word “digits” in quotation marks, because... It doesn't just use numbers. And really, how can that be? Let me explain: from 0 to 9 we count in the same way as in decimal, but then it will be like this: A, B, C, D, E, F. The number F is not difficult count, it will be equal to 15 in the decimal system (see Table 1).

Decimal number

Hexadecimal number

Table 1. Decimal and hexadecimal systems.

Thus, if we add a zero to the right of any number in the hexadecimal system, then this number will increase by16 once.

Example 1: 1 x 16 = 10; 10 x 16 = 100; 100 x 16 = 1000, etc.

Were you able to distinguish hexadecimal numbers from decimal numbers in Example 1? And from this series: 10, 12, 45, 64, 12, 8, 19? These can be either hexadecimal or decimal. In order to avoid confusion and the computer to be able to clearly distinguish one number from another, in assembler it is customary to place the symbol h or H after a hexadecimal number ( H is an abbreviation for English. hexadecimal (hexadecimal). For brevity, it is sometimes simply called Hex ) . And don’t put anything after the decimal. Because numbers from 0 to 9 in both systems have the same meanings, then the numbers written as 5 and 5h are the same.

That. Example 1 (see above) would be more correct to write like this: 1 x 16 = 10h; 10h x 16 = 100h; 100h x 16 = 1000h. Or like this: 1h x 10h = 10h; 10h x 10h = 100h; 100h x 10h = 1000h.

We will look at why the hexadecimal system is needed in subsequent issues. For now, for our example program, which will be discussed below, we need to know about the existence of hexadecimal numbers.

So, let's summarize. The hexadecimal number system consists of 10 digits (from 0 to 9) and 6 letters of the Latin alphabet (A, B, C, D, E, F). If we add a zero to the right of any number in the hexadecimal system, then this number will increase by16 once. It is very important to understand this topic, since we will constantly use it when writing programs.

Now a little about how I will build examples in Assembly. It is not entirely convenient to present them in HTML format, so first there will be the program code itself with numbered lines, and immediately after it there will be explanations and notes.

Like that:

lines Program code
(1) mov ah,9

Explanations:

In line (1) we do this, and in line (15) we do that.

Huge request: DO NOT copy programs from a page to the clipboard and then paste them into Notepad (or anywhere else)! Retype them manually in a text editor. If you have a printer, then select the program, print the selected fragment, and then transfer it into the editor from paper. All examples must be typed yourself! This will speed up the memorization of operators.

And further. There is no difference between lowercase and UPPERCASE letters in assembler. Records of the form:

The assembler perceives them the same way. You can, of course, force the assembler to distinguish between lowercase and UPPERCASE characters, but we will not do this for now. To make the program easier to read, it is best to type operators in lowercase letters, and start the names of subroutines and labels in uppercase letters. But it depends on who will be comfortable.

So, let's move on to our first program:

(1) CSEG segment

(2)org 100h

(4)Begin:

(6) mov ah,9

(7) mov dx,offset Message

(8) int 21h

(10) int 20h

(11)

(12) Message db "Hello, world!$"

(13)CSEG ends

(14) end Begin

In order to explain all the operators in this example, we will need several editions. Therefore, we will simply omit the description of some commands at this stage. Just assume that this is how it should be. We will look at these operators in detail in the very near future. So, lines numbered (1), (2) and (13) you simply ignore.

Lines (3), (5), (9) and (11) are left blank. This is done for clarity. The assembler will simply omit them.

Now let's move on to consider the remaining operators. The program code begins with line (4). This is a mark that tells the assembler to the beginning of the code. Line (14) contains the operators end Begin ( Begin English Start; end end). This is the end of the program. In general, instead of the word Begin something else could have been used. For example, Start:. In this case, we would have to end the program End Start (14).

Lines (6) (8) display the message Hello, world!. Here we will have to briefly talk about processor registers (we will look at this topic in more detail in the next issue).

A processor register is a specially allocated memory for storing a number.

For example:

If we want to add two numbers, then in mathematics we write it like this:

A, B and C these are a kind of registers (if we talk about a computer) in which some data can be stored. A=5 can be read as: Assign A the number 5 .

To assign a register a value, there is a mov operator in Assembler (from the English move load). Line (6) should be read like this: Loading into the register A.H.number 9 (in other words, we assign A.H.number 9). Below we will look at why this is necessary.

In line (7) we load into the register DX message address for output (in this example it will be the stringHello, world!$).

Interrupts will be covered in detail in subsequent issues. Here I will say a few words.

Interrupt MS-DOS it is a kind of subroutine (part MS-DOS), which resides permanently in memory and can be called at any time from any program.

Let's consider the above using an example (Notes in small print):

Program for adding two numbers

HomePrograms

A=5 We enter the value 5 into variable A

B=8 to variable B the value 8

Calling Subroutines Addition

now C equals 13

A=10 same thing, just different numbers

B=25

Calling Subroutines Addition

now C is equal to 35

End of the Program

Subroutine Addition

C=A+B

ReturnFromSubroutine we return to the place from which we called

EndSubroutine

In this example, we called the subroutine twice Addition, which added two numbers passed to it in variables A and B . The result is placed in the variable C. When a subroutine is called, the computer remembers where it was called from, and then, when the subroutine has finished running, the computer returns to the place from which it was called. That. You can call subroutines an indefinite number of times from anywhere.

When executing line (8) of an Assembly program, we call a subroutine (in this case called an interrupt), which displays the line on the screen. For this purpose, we, in fact, place the necessary values ​​in registers. All the necessary work (outputting a line, moving the cursor) is taken over by the subroutine. This line can be read like this: call the twenty-first interrupt ( int from English interrupt interrupt). Please note that after the number 21 there is a letter h . This, as we already know, is a hexadecimal number (33 in decimal). Of course, nothing prevents us from replacing the line int 21h to int 33. The program will work correctly. It's just common practice in Assembler to indicate the interrupt number in hexadecimal.

In line (10) we, as you may have guessed, call interrupt 20 h . To call this interrupt, you do not need to specify any values ​​in the registers. It performs only one task: exiting the program (exiting to DOS). As a result of the execution of interrupt 20h, the program will return to where it was launched from (loaded, called). For example, in Norton Commander or DOS Navigator.

Line (12) contains the message to be output. First word ( message message) message title. It can be anything (for example, mess or string, etc.). ABOUT Pay attention to line (7), in which we load into the register DX our message address.

We can create another line, which we will call Mess2. Then, starting from line (9), insert the following commands:

(10) mov dx,offset Mess2

(13) Message db "Hello, world!$"

(14) Mess2 db "It's ME! $"

and reassemble our program. I hope you can guess what's going to happen

Pay attention to the last character in the lines Message and Mess2 - $. It points to the end of the line. If we remove it, then 21 h the interrupt will continue outputting until it encounters a character somewhere in memory $. On the screen we will see garbage .

If you have a debugger, you can see how our program will work.

The purpose of this issue was not to understand in detail with each operator. This is impossible, because you don't have enough knowledge yet. I believe that after 3-4 releases you will understand the principle and structure of an Assembly program. Perhaps the Assembly language seemed extremely complicated to you, but believe me, this is at first glance.

Now there is a very easy walk ahead associated with the hexadecimal number system. In this case, we hope you suspect, and probably rightly so, that we should now have 16 different digits.

But, as we know, there are only ten traditional (“Arabic”) numerals. And it takes sixteen. It turns out that six characters are missing.

Comment
Thus, a purely design task arises on the topic “Signs” - to come up with the missing symbols for the numbers
.

This means that at one time specialists needed to come up with some new signs. But once upon a time, at the beginning of the computer era, there was not much choice in signs. Programmers only had numbers and letters at their disposal. Therefore, they took the elementary path: they took the first letters of the Latin alphabet as numbers, especially since historically this was not the first time (we have already mentioned that initially many peoples used letters instead of numbers).

Comment
We hope that everyone understands why in this case it is impossible to use, for example, the numbers “10”, “11”, “12”, etc.? Because if we are talking about the hexadecimal number system, then it should be sixteen numbers, not numbers
.

And the decimal number “10” began to be denoted by the Latin letter “A” (more precisely, “number A”). Accordingly, the numbers “B”, “C”, “D”, “E” and “P” come next.

Since we intended to build a hexadecimal system, starting from zero, this is exactly where we get 16 digits. For example, the digit "D" is the decimal number "13", and the digit "F" is the decimal number "15".

When we add one to the hexadecimal number “F”, then, since we have run out of these digits, we put “O” in this digit, and transfer one to the next digit, so it turns out that the decimal number “16” will be represented in the hexadecimal number system by the number "10", i.e. it turns out to be a "hexadecimal ten". Let's combine decimal and hexadecimal numbers into a single table (Table 4.5).

Table 4.5. Matching decimal and hexadecimal numbers.

Decimal number Hexadecimal number Decimal number Hexadecimal number
0-9 0-9 29 1D
10 A 30 1E
11 IN 31 1F
12 WITH 32-41 20-29
13 D 42-47 2A-2F
14 E 48-255 30-FF
15 F 256 100
16 10 512 200
17-25 11-19 1024 400
26 1A 1280 500
27 1B 4096 1000
28 1C

The hexadecimal system is used to more compactly record binary information. In fact, a "hexadecimal thousand", consisting of four digits, occupies thirteen digits in binary (1000 16 = 1000000000000 2).

When discussing number systems, “tens”, “hundreds” and “thousands” have repeatedly appeared, so it is necessary to pay attention to the so-called “round” numbers.

Hexadecimal notation ("Hex")- a convenient way to represent binary values. Just as the decimal number system has a base of ten and the binary number system has a base of two, the hexadecimal number system has a base of sixteen.

The base 16 number system uses the numbers 0 to 9 and the letters A to F. The figure shows the equivalent decimal, binary, and hexadecimal values ​​for the binary numbers 0000 to 1111. We find it easier to express a value as one hexadecimal digit than as four bits

Understanding Bytes

Given that 8 bits (bytes) are the standard binary grouping, the binary numbers 00000000 to 11111111 can be represented in hexadecimal notation as the numbers 00 to FF. Leading zeros are always displayed to complete the 8-bit representation. For example, the binary value 0000 1010 in hexadecimal would be 0A.

Representation of Hexadecimal Values

Please note: It is important to distinguish hexadecimal values ​​from decimal values ​​for characters 0 through 9, as shown in the figure.

Hexadecimal values ​​are usually represented in text by a value preceded by 0x (such as 0x73), or by using a subscript of 16. Less commonly, they may be followed by the letter H, such as 73H. However, because subscript text is not recognized on the command line or in programming environments, they are preceded by "0x" (zero X) in the technical representation of hexadecimal numbers. Therefore, the examples above would be shown as 0x0A and 0x73 respectively.

Hexadecimal notation is used to represent Ethernet MAC addresses and IP Version 6 addresses.

Hexadecimal Conversions

Converting numbers between decimal and hexadecimal values ​​is simple, but quickly dividing or multiplying by 16 is not always convenient. If such conversions are necessary, it is usually easier to convert a decimal or hexadecimal value to binary, and then convert the binary value to decimal or hexadecimal, depending on what you want to get.

With practice, it is possible to recognize the binary bit patterns that correspond to decimal and hexadecimal values. The figure shows these patterns for some 8-bit values.