CodeItBetter Programming Another VB Programming Blog

How to Convert the number n to any base between 2 and 36

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'Math - How to Convert the number n to any base between 2 and 36
Option Explicit
 
Public Function Long2Base(ByVal n As Long, ByVal nB As Integer) As String
    Dim s As String
    Dim nD As Integer
    Dim Negative As Boolean
    Negative = n < 0
    n = Abs(n)
    Do
        nD = n Mod nB
        If nD > 9 Then
            nD = nD + 7
        End If
        s = Chr$(48 + nD) & s
        n = n \ nB
    Loop Until n = 0
    If Negative Then
        s = "-" & s
    End If
    Long2Base = s
End Function
Filed under: Math Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.