+ Reply to Thread
Results 1 to 7 of 7

Thread: Leading Zeros for Base 10 to Base 36 Converter

  1. #1
    Registered User
    Join Date
    07-06-2011
    Location
    Utah
    MS-Off Ver
    Excel 2003
    Posts
    2

    Leading Zeros for Base 10 to Base 36 Converter

    Hi all,

    Tried to respond to your posts, but it keeps saying that my message is too short even though it is longer than the 10 characters required. Sorry.

    I got this vb conversion program from Tony who posted on Excel Forum. The program converts base 10 to base 36. The program works great. I just need it to add leading zeros in front of the base 36 number it generates. Does any one know how to do this? See the program below, thanks
    :


    Function Dec2Base(ByVal vDecimalNumber As String, ByVal vBase As Integer) As String
     ' convert a decimal to any base (up to 36)
     ' by tonywig
     Dim vRemainder     As Integer
     Dim vRemainderChar As String
     Dim vLeftToDivide  As Long
     Dim vDec2Base     As String
     Dim vLooper        As Integer
    
     If vBase < 2 Or vBase > 36 Then Err.Raise 5
     
     vLeftToDivide = Val(vDecimalNumber)
     vDecToBase = ""
       
     vLooper = 1
     
     While vLooper = 1
           vRemainder = vLeftToDivide - Int(vLeftToDivide / vBase) * vBase
           vRemainderChar = Mid$("123456789ABCDEFGHIJKLMNPQRSTUVWXYZ0", vRemainder, 1)
           vDec2Base = Trim(vRemainderChar) & vDec2Base
           vLeftToDivide = Int(vLeftToDivide / vBase)
           If vLeftToDivide = 0 Then vLooper = 0
           
     Wend
    
    Dec2Base = vDec2Base
    
    
    End Function
    Last edited by norman.johnson; 07-07-2011 at 10:11 AM. Reason: Didn't follow forum rules

  2. #2
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: norman.johnson

    So much wrong with your post. Expect to get lit up by mods

  3. #3
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: norman.johnson

    Hi norman.johnson and welcome to the forum. Please take a moment to read the forum rules located here and amend your title as per Rule #1. Also, you must wrap your code in code tags as per Rule #3. Once you do that, someone will be able to help you.
    Please leave a message after the beep!

  4. #4
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: Leading Zeros for Base 10 to Base 36 Converter

    I am not all that versed in the difference between Base 10 and Base 36. Just did a wikipedia search. I am not sure if the Base 36 by definition should have zeroes in front of it that is getting cut off or if you just want to add a zero in front of it. If the latter it might be a formatting issue or you could try the untested change below.

    Change
    Dec2Base = vDec2Base
    To:
    Dec2Base = "0" & vDec2Base

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Leading Zeros for Base 10 to Base 36 Converter

    Here's similar UDF with the ability to specify the length:
    Function DblToBase(ByVal d As Double, _
                       iBase As Long, _
                       Optional iLen As Long = 0) As String
        ' shg 2007-0807
        ' Returns the string conversion of positive Double d to the specified iBase (2..36)
        Const s         As String = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
        Static sSym     As Variant
    
        If IsEmpty(sSym) Then sSym = Split(s, ",")
    
        If iBase < 2 Or iBase > 36 Then
            DblToBase = "Invalid base!"
    
        ElseIf d < 0 Or d > 1E+15 Then
            DblToBase = "Invalid number!"
    
        Else
            Do
                DblToBase = sSym(d - Int(d / iBase) * iBase) & DblToBase
                d = Int(d / iBase)
            Loop While d
            
            If Len(DblToBase) < iLen Then
                DblToBase = String(iLen - Len(DblToBase), "0") & DblToBase
            End If
        End If
    End Function
    E.g., =DblToBase(1E15, 36, 12) returns 009UGXNORJLS
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    07-06-2011
    Location
    Utah
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Leading Zeros for Base 10 to Base 36 Converter

    Quote Originally Posted by stnkynts View Post
    I am not all that versed in the difference between Base 10 and Base 36. Just did a wikipedia search. I am not sure if the Base 36 by definition should have zeroes in front of it that is getting cut off or if you just want to add a zero in front of it. If the latter it might be a formatting issue or you could try the untested change below.

    Change
    Dec2Base = vDec2Base
    To:
    Dec2Base = "0" & vDec2Base
    Thanks Stnkyts, that worked.

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Leading Zeros for Base 10 to Base 36 Converter

    If you are satisfied with the solution(s) provided, please mark your thread as Solved. Do not report the post, you can also give the person Rep points

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0