How to Convert a number in any base from 2 to 36 to a long
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 23 24 25 26 27 28 29 30 31 32 33 34 35 | 'Math - How to Convert a number in any base from 2 to 36 to a long Option Explicit Public Function Base2Long(ByVal s As String, ByVal nB As Integer) As Long Dim S2 As String, S3 As String Dim I As Long, J As Long, X As Long Dim N As Boolean If Len(s) < 1 Then Base2Long = 0 Exit Function End If S2 = UCase$(s) If Left$(S2, 1) = "-" Then N = True S2 = Right$(S2, Len(S2) - 1) Else N = False End If J = 1 X = 0 For I = Len(S2) To 1 Step -1 S3 = Mid$(S2, I, 1) Select Case S3 Case "0" To "9": X = X + J * (Asc(S3) - 48) Case "A" To "Z": X = X + J * (Asc(S3) - 55) End Select J = J * nB Next I If N Then X = -X End If Base2Long = X End Function |