Better coding techniques.
Your method looks okay. I too, got the correct answer on a hand calculator, so you are probably counting one too many or one too few hex characters.
I think the general approach provided by a previous post is not a good way to go.
I am too lazy to write a lot of code, but will supply some hints.
First. I would not use a case statement to translate the hex characters.
Code:
Public Const HextString As String*16 = “0123456789abcdef”
. . . .
Dim Digit as Integer ‘Value of a Hex digit
Dim CharPosition as Integer ‘Assume this is position of Hex character in string
Dim HexString as String ‘Assume this is string containing hex characters’
Dim HexCharacter as String*1
HexCharacter = Mid(HexString, CharPosition, 1) - 1
Digit = Instr(HexString, HexCharacter)
Next, integer radix conversion is exactly like evaluating a polynomial. When converting from Hex, you are evaluating a polynomial in X for X = 16, where the coefficients are the values of the Hex Digits. This is best done without evaluating high exponential values. For example, to convert AF34 to decimal, use a loop to evaluate the following algebraic expression.
Code:
[(10*16 + 15)*16 + 3]*16 + 4
Note that the Hex digit A was implicitly multiplied by 16^3, without actually calculating 16^3.
BTW: When evaluating polynomials, it is essential to use the above technique. There are polynomials for which you can get imprecise results by calculating all the powers of the independent variable and adding the individual terms. This is especially true for polynomials like the series for sine, cosine, exponentials, et cetera.