PDA

View Full Version : Convert a Number in Any Base (Up To 36) to a (Very Large) Positive Decimal Number



Rick Rothstein
06-26-2012, 09:32 PM
See this article for the complimentary function Dec2Base...
Convert a (Possibly) Very Large Decimal Number to Any Base (Up To 36) (http://www.excelfox.com/forum/f22/convert-possibly-very-large-decimal-number-any-base-up-36-a-878/)

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.


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...


MsgBox Base2Dec("10011011011101011010110110101", 2) ==> 326022581

MsgBox Base2Dec("FFFFFFFFFFFFFFFFFFFFFFFF", 16) ==> 79228162514264337593543950335

MsgBox Base2Dec("Z2KS69UIAK", 36) ==> 3561869315733788

SDruley
12-30-2012, 01:54 PM
Well, Rick,

Believe it or not I find this useful

Steve

Rick Rothstein
12-31-2012, 06:46 AM
Well, Rick,

Believe it or not I find this useful

Steve
GREAT! I am glad you are finding a use for it. Thanks for letting me know.

Rick Rothstein
03-31-2013, 06:18 AM
I am replying to this thread to alert anyone who is subscribed to it that I added a note at the top of this article with a link to an article that I just posted which contains the companion function (Dec2Base) to the one posted in this article.