Hex To Decimal
This program converts a hexadecimal number to a decimal number.

You can get a hexadecimal number from a decimal number by multiplying each digit to the power of 16 from its corresponding place and adding them together.

Line 20 asks for user to input their hexadecimal number and stores it in string H$.

Line 30 takes the length of string H$, subtracts one from it and stores it in L.

Lines 40 to 90 reads one character at a time from the H$ string. It then gets the ASCII value of the character and stores it in C. It then checks to see if C is a number between 48-57 (0-9) or 65-70(A-F). If it is a number, it subtracts 48 from the ASCII value to get its hexadecimal value. If it is a letter, it subtracts 55. It then multiplies the resulting value with 16 to the power of L (the digit's place) and adds it to D, the sum. L is subtracted by one to get the next digit's place.

For example:

Hexadecimal #: 1B8

L equals the length of H$ - 1. The length of "1B8" is 3, so L = 2.

The first character of string H$ is "1". The ASCII value of "1" is 49. Since it is a number between 48 and 57,and therefore a number, it is subtracted by 48 which gives the value 1. 1 * 16 ^ L(2) equals 256. So, D = D(0) + 256.

D = D + ((C - 48) * 16 ^ L)

D = 0 + ((49 - 48) * 16 ^ 2)

D = 0 + ((1) * 16 ^ 2)

D = 256

L = L(2) - 1 which is 1.

The second character of string H$ is "B". The ASCII value of "B" is 66. Since it is a number between 65 and 70,and therefore a letter, it is subtracted by 55 which gives the value 11. 11 * 16 ^ L(1) equals 176. D = D(256) + 176 which equals 432.

D = D + ((C - 55) * 16 ^ L)

D = 256 + ((66 - 55) * 16 ^ 1)

D = 256 + ((11) * 16 ^ 1)

D = 432

L = L(1) - 1 which is 0.

The third character of string H$ is "8". The ASCII value of "8" is 56. Since it is a number, it is subtracted by 48 which gives the value 8. 8 * 16 ^ L(0) equals 8. So D = D(432) + 8 which equals 440.

D = D + ((C - 48) * 16 ^ L)

D = 432 + ((56 - 48) * 16 ^ 0)

D = 432 + ((8) * 16 ^ 0)

D = 440

The decimal value of the hexadecimal 1B8 is 440.

Great source for conversions with explanations.

https://www.rapidtables.com/convert/num ... cimal.html
Code: Select all

```
1 REM --- HEX TO DECIMAL CONVERTER
10 PRINT "HEX TO DECIMAL"
20 INPUT "ENTER HEX #"; H$
30 L = LEN(H$) - 1
40 FOR I = 1 TO LEN(H$)
50 C = ASC(MID$(H$, I, 1))
60 IF C >= 48 AND C <= 57 THEN D = D + ((C - 48) * 16 ^ L)
70 IF C >= 65 AND C <= 70 THEN D = D + ((C - 55) * 16 ^ L)
80 L = L - 1
90 NEXT I
100 PRINT D
```