Results 1 to 5 of 5

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

Threaded View

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

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

    Back in my "Convert a Number in Any Base (Up To 36) to a (Very Large) Decimal Number" article, I posted a function that would convert a number in any base to a decimal number where that number could contain up to 29 decimal digits... the following function is the compliment to it... with it, you can convert any decimal value up to a maximum of 79228162514264337593543950335 to any base (up to 36). The function takes two arguments... the decimal number you want to convert and the base you want to convert it to. Because VB will want to convert numbers that are too large to scientific notation, you will need to pass such large values into the function as a text string... smaller numbers can be passed in as numbers or text strings.
    Code:
    Function Dec2Base(DecimalValue As Variant, Base As Long) As String
      Const PossibleDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      DecimalValue = CDec(DecimalValue)
      Do Until DecimalValue = 0
        Dec2Base = Mid(PossibleDigits, CDec(DecimalValue) - Base * _
                   Int(DecimalValue / Base) + 1, 1) & Dec2Base
        DecimalValue = Int(CDec(DecimalValue) / Base)
      Loop
    End Function
    Here are a few examples to give you an idea of its use...
    Code:
    MsgBox Dec2Base("326022581", 2)  ==>  10011011011101011010110110101
    
    MsgBox Dec2Base("79228162514264337593543950335", 16)  ==>  FFFFFFFFFFFFFFFFFFFFFFFF
    
    MsgBox Dec2Base("3561869315733788", 36)  ==>  Z2KS69UIAK
    Last edited by Rick Rothstein; 04-30-2017 at 12:25 AM.

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2013, 06:18 AM
  2. Read/write very large xl2007 files
    By Rasm in forum Excel Help
    Replies: 3
    Last Post: 04-07-2012, 05:28 AM
  3. Replies: 4
    Last Post: 03-10-2012, 07:15 PM
  4. VBA Function To Extract Decimal Numbers
    By PcMax in forum Excel Help
    Replies: 7
    Last Post: 11-19-2011, 09:42 PM
  5. Unique Large Values From Duplicate List
    By S M C in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 10-04-2011, 02:17 AM

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
  •