Transfer from binary system into a decimal system

If we want to program a calculator to transfer between binary and decimal systems, we will have to ampify the number two. We will use the Power function to amplify it. With this feature, we will be able to boost the number to any number. Since Power is a mathematical function, it works with Real Numbers a thus the output from it must be a Real- type variable. The power function is composed of two parameters - the first parameter is the number that we want to amplify, the second parameter is the power, e. g. the result of Power (5, 4); will be 625 (54).

Since Power is not included in default (preset) libraries when starting a new application in Lazarus, but it is contained in the library related to mathematics (Math), we must write it among other libraries (Figure 99).

Figure 99: Library addition
Figure 99: Library addition

To create a conversion calculator, use the edit line to enter the number in binary system. Subsequently, the created program recalculates it into a decimal system and display it on canvas.

As the next step we declare a variable number into which we load the value from the Edit1 component. We know that the edit line works with text, that is, the used data type will be String. We also need to declare the result variable. As we work with Power, the variable result must be of Real data type (Figure 100).

Figure 100: Declaration of variables
Figure 100: Declaration of variables

In order for the program to recalculate all the given numbers and to write them out as a single number, we use the For loop and also the Length command. Based on the example above, we know that we multiply the amplified two by zero or one. We know that the result of multiplication by zero (0 * 22) is equal to zero, so we can omit the zero and we will only calculate with ones.

The loop we entered will go through the entered number and if it finds the digit one, the number two will be amplified to the number n-1, where n is the serial number of the given digit one in our number. From the mathematics we know that multiplication by a one does not affect the final result, so we can omit the multiplication by itself and we will only amplify the number two. The last step of the calculation is the sum of all the powers, and we get the final value in the decimal system. Finally, we let the result appear on the graphical area. In order to be able to display the given number on the screen, we must again convert the numeric value to text. Therefore, we will use the command to convert FloatToStr, that is, convert the real number into a character string (Figures 101 and 102).

Figure 101: Converting binary system to decimal
Figure 101: Converting binary system to decimal
Figure 102: Result transfer binary system to decimal
Figure 102: Result transfer binary system to decimal
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