Transfer from decimal system into a hexadecimal system
We will now program this procedure in Lazarus. As with the previous examples, we will need a button, graphical area, and edit line to enter the numbers we want to convert. To create the program, we use the method of division by the basis. Embedded numbers from edit component will be divided by 16 until the result is zero. From the examples above, we know that the result is division remainders written from last to first.
First, we declare a variable number, into which we load the number in decimal system. This number will be converted from text data type ( String ) to Integer data type using StrToInt conversion . Since we want the program to divide the inserted number by 16 until the integer result is zero, we use the Repeat loop and enter the condition when the number after the last division is zero. As with the conversion from decimal to binary and vice versa, we need this program to be able to work with continuous result and to divided it. It is also necessary to remember the remainder of the division, to which we will add additional remainders. For this purpose, we will use the newly created variable result, which will be of the String data type because we do not need to work with it as with a numerical data, but textual data for writing to the graphical area.
Number entered in edit line will be divided twice. First, to get an integer, that is, a number that is to the left of the decimal point and we will continue to use it for repeated division. In this division we will use the div command. The second time we divide by the mod command to get the rest after an integer division, that is, the number written to the right of the decimal point, in our case the final result. As well as in previous transfers, the results will always be written to the left to transfer result. Again, we must not forget the correct order of the commands (Figure 103).
After running a program we will see the following solution (Figure 104):
Based on the example we have calculated above we see that the result is not correct (9410 = 5E16). The problem occurs in the moment when the remainder of the division is greater than nine. At that time, the program will add the rest to the previous (current) remainder, but in the hexadecimal system we do not work with numbers only but also letters from A after F and thus the 10 to 15 remainders must be replaced by these letters. Therefore, we need to add a command to our program that swaps the numbers for the corresponding letters for the remainder above nine. The simplest solution will be to use the Case command. We will also create rest variable to which we will write the current remainder. This will be compared with conditions specified in the Case statement. If we compare the variable result, the program would have a problem with comparisons (Figures 105 and 106).