Transfer from hexadecimal system into a decimal system
We now program the number conversion process into a calculator. Once again, we define variables - the variable number to which we will enter the numeric input in hexadecimal system from the edit line, and the variable result for continuous calculations and the final correct result. We need the variable number in string data type (we will not convert it to an integer data type) because we know that the hexadecimal system also uses six letters of the alphabet and thus, the values specified in a combination with letters could not be converted to and the program would report a mistake. The result variable must again be of Real data type, since it will work in calculations of powers and from the previous chapter, we know that Power works with real numbers. Also, we must not forget to add the Math library to the current libraries in our application.
First, we load from the Edit1 component a value and we save it to the number variable. In order for the program to transfer all of the numbers you entered to a unified result, we use the For loop and Length command. The program will go through the individual numbers (or letters) entered in the edit line until it reaches the last. Based on the example above we know that the value of a digit or a character is multiplied by n-1 by the power of 16. Finding the value of numbers will not be a problem, since the value of one is 1, the value of two is 2, and so on. The problem is when a loop encounters a non-numeric character (a letter). Therefore, we need to distinguish whether a program has read a number or a letter. If it has retrieved a letter, it must assign a value to multiply the current power of 16.
To distinguish numbers from letters, enter a simple If statement that if the current character is a number (it belongs to the closed interval 0 to 9), it multiplies the current power of the number 16 by the number that is entered in the given position (if we entered the fourth character number 8, the program multiplies by 8 the third power of 16 => 163 * 8), adds to current result and the new result is recorded in the variable result (Figure 107).
If the specified character is a letter, the program must distinguish, which character it is and on the basis of it, the power of 16 will be multiplied by values from 10 to 15. For this distinction, we will use the else branch, in which we use the Case command (using the Case command will be in this is easier than using the If command). In the Case statement we will compare the current value of the character number[I] and for each possible solution, we propose a multiple value to be applied in the calculation (A = 10, B = 11, C = 12, etc. ). For this purpose, we create a new integer variable times, which we assign a value from 10 to 15 and then we will multiply it by the power of 16.
In order not to write the calculation, we will use our current For loop and at the end of each cycle, we enter its conversion, which remembers as the current value of the variable result to which it added its previous value. Therefore, in the If branch, we remove the conversion of result and only enter the value of the variable times as a number that is entered at the current position of the detected digit (number [I] ).
The problem may occur when you run the program in a step of character matching. As we want to compare the letters A to F in the case statement, we need to write them in single quotation marks so that the program can compare them as characters (Figure 108).
Finally, we will list the final result. For comparison, enter the number from the example above and after starting the program we get the correct conversion (Figures 109 and 110).