HexToDec




Const HexA = 10
Const HexB = 11
Const HexC = 12
Const HexD = 13
Const HexE = 14
Const HexF = 15

Public Function ConvertHexToDecimal(ByVal HexValue As String) As Long
Dim rValue, A As Long
Dim Temp, Rev As String

Rev = StrReverse(HexValue)
'Numbers are read from right to left, unlike text, which

'are read from right to left; therefore, the string should

'be reversed so it can be read like a normal string.


For A = 1 To Len(HexValue)
Temp = Mid$(Rev, A, 1)
'This, along with the for-next loop allows you to

'read all the characters in the screen 1 at a time


If Val(Temp) = Temp Then
'Character is a number

If A = 1 Then
'Character is a number, and is the first

'character in the string

rValue = Val(Temp)
'Because the character is the first of the

'string, there is no value in rValue yet, so

'you can just assign the value

Else
rValue = rValue + (Val(Temp) * (16 ^ (A - 1)))
'So this adds to rValue.. it takes it's

'current value, and adds to it, so the

'previous value isn't lost. Because single

'digit hex values can be up to a value of 15,

'the decimal value of 10 would be 16. Now,

'values are not their own when they are not

'their own if they aren't the first character

'of the string. Here, we use exponents..

'if 16^0 were 0, then I would have done this

'diffrently, but because it isn't, it has to

'be it's position(a) -1. The -1 is because

'you don't multiply by 16 on the first

'character, you start it on the 2nd character.

End If
Else
Select Case LCase$(Temp)
'Because single digit hex values can go up to

'15, more characters were needed, so A - F

'were added in. A has the value of 10, B has

'the value of 11, and so forth. G is not a

'valid character because it is a 15 value

'system. Here, it just goes through the

'valid letters, and does the same thing it did

'with numbers.

Case "a"
If A = 1 Then
rValue = HexA
Else
rValue = rValue + (HexA * (16 ^ (A - 1)))
End If
Case "b"
If A = 1 Then
rValue = HexB
Else
rValue = rValue + (HexB * (16 ^ (A - 1)))
End If
Case "c"
If A = 1 Then
rValue = HexC
Else
rValue = rValue + (HexC * (16 ^ (A - 1)))
End If
Case "d"
If A = 1 Then
rValue = HexD
Else
rValue = rValue + (HexD * (16 ^ (A - 1)))
End If
Case "e"
If A = 1 Then
rValue = HexE
Else
rValue = rValue + (HexE * (16 ^ (A - 1)))
End If
Case "f"
If A = 1 Then
rValue = HexF
Else
rValue = rValue + (HexF * (16 ^ (A - 1)))
End If
End Select
End If
DoEvents
'ALWAYS have this in loops.. you don't know how slow

'the computer it's being operated on is, the lack of

'DoEvents could cause the computer freeze up

'temporerily, or even force you to force shutdown.

Next A
ConvertHexToDecimal = rValue
'Sends the value that has been made out :)

End Function











( hextodec.html )- by Paolo Puglisi - Modifica del 17/12/2023