See this article for the complimentary function Dec2Base...
Convert a (Possibly) Very Large Decimal Number to Any Base (Up To 36)
This is probably more of a novelty function than anything else, but I figured I would share it with the readers of this forum anyway. The function will take any positive number in any base up to Base 36 and convert it into a Decimal value. The main point of interest for this function is the size of the numbers it can handle. The maximum Decimal value that can be returned by the function is 79228162514264337593543950335... that is a 29-digit number which, I'm thinking, is well beyond any value you would ever need to calculate (hence my description of it as a "novelty function").
Anyway, here is the function. It takes two arguments... the number in "base" digits and the actual base to use.
Code:
Function Base2Dec(BaseDigits As String, BaseNumber As Long) As Variant
Dim X As Long, Z As Long, DigitVal As Variant, Power As Variant
Const PossibleDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If Not UCase(BaseDigits) Like "*[!" & Left(PossibleDigits, BaseNumber) & "]*" Then
For X = 0 To Len(BaseDigits) - 1
DigitVal = UCase$(Mid(BaseDigits, Len(BaseDigits) - X, 1))
DigitVal = InStr(PossibleDigits, DigitVal) - 1
Power = 1
For Z = 1 To X
Power = CDec(Power) * BaseNumber
Next
Base2Dec = Base2Dec + DigitVal * Power
Next
Else
Err.Raise 9999, , "Bad base digit specified"
End If
End Function
Here are a few examples to give you an idea of its use...
Code:
MsgBox Base2Dec("10011011011101011010110110101", 2) ==> 326022581
MsgBox Base2Dec("FFFFFFFFFFFFFFFFFFFFFFFF", 16) ==> 79228162514264337593543950335
MsgBox Base2Dec("Z2KS69UIAK", 36) ==> 3561869315733788
Bookmarks