Results 1 to 4 of 4

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

  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13

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


    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
    Last edited by Rick Rothstein; 03-31-2013 at 07:39 PM.

  2. #2
    Junior Member SDruley's Avatar
    Join Date
    Nov 2012
    Posts
    23
    Rep Power
    0
    Well, Rick,

    Believe it or not I find this useful

    Steve

  3. #3
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by SDruley View Post
    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.

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    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.

Similar Threads

  1. Convert a (Possibly) Very Large Positive Decimal Number to Any Base (Up To 36)
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 4
    Last Post: 04-22-2013, 04:52 PM
  2. Adding specfic number to a available number in previous cell.
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 2
    Last Post: 03-02-2013, 01:55 AM
  3. finding the number of occurrence
    By zzzqinzzz in forum Excel Help
    Replies: 2
    Last Post: 12-13-2012, 10:24 AM
  4. Replies: 4
    Last Post: 03-10-2012, 07:15 PM
  5. Number to Words (Rupees)
    By sa.1985 in forum Excel Help
    Replies: 2
    Last Post: 12-16-2011, 08:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •