Transfer from decimal system into a binary system

Now we program the transfer calculator. We will need a button, an edit line to write the decimal number that we want to convert to the binary and the graphical area where the results will be displayed. To create the program, it will be easier to use the method of dividing by the base. We know that the numbers will be divided by two until the result is zero. We also know that the result is the remainder of the division from last to first.

First, we declare a variable number in which we load the number in the decimal system. This will be of an integer type. As we read it from an edit line that works with the String data type, we will have to convert it to an integer data type using the StrToInt command (Figure 94).

Figure 94: Transfer units
Figure 94: Transfer units

Since we want the program to divide the number by two until the integer result is zero, we use the Repeat and we give it a condition (the number after the last division is zero) . So that the program could work with continuous result and divided it further, we will rewrite the originally entered number after the division. It is also necessary to remember the remainder of the division, to which we will attribute additional remainders. We create a new variable result for this purpose. Because we just want to write it down at the end of the division, we will not count with it, it will be of String data type.

In this moment we know that we can divide by two commands - div and mod . By the command div we get an integer, that is, a number that is written to the left of the decimal point. On the contrary, using the command mod, we get the rest after an integer division, that is, the number written to the right of the decimal point. We'll use both of these commands in our benefit in programming our calculator.

We want the number written in the edit line to be divided by two and at the same time we want the integer result to be written as the new number that will be divided in the next cycle. Next, we want the remainder of the division to be written into the result and the next result before the previous result (to the left of it) (Figures 95 and 96).

Figure 95: Process preview
Figure 95: Process preview
Figure 96: Incorrect result
Figure 96: Incorrect result

If we write commands exactly in this order, an error occurs. Although the program correctly divides the numbers and writes the result, the final result is incorrect and even starts with the number zero that does not write any number system at the beginning of the number (even if it is there). Let's write the program calculation procedure:

  1. We have entered the program number 10. This number is divided by two, the result is 5. At the same time, number 5 is remembered as the next number for division. 
  2. The program will again divide the number by two and remembers the remainder of the division. But! In this step the program already works with the changed number, hence it works with number 5, not with the number 10. Therefore, the remainder is 1 (5: 2 = 2, rest 1, 10: 2 = 5, rest 0), instead of number 0. 

To avoid this problem, we have to replace these two lines (Figure 97). 

Figure 97: Changed code
Figure 97: Changed code

Now the program will do the correct conversion of the numbers from the decimal system to the binary system (Figure 98).

Figure 98: Right transfer result from decimal system to binary
Figure 98: Right transfer result from decimal system to binary
Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky