56 lines
1.4 KiB
Text
56 lines
1.4 KiB
Text
' FB 1.05.0 Win64
|
|
|
|
Function min(x As Integer, y As Integer) As Integer
|
|
Return IIf(x < y, x, y)
|
|
End Function
|
|
|
|
Function convertToBase (n As UInteger, b As UInteger) As String
|
|
If n < 2 OrElse b < 2 OrElse b = 10 OrElse b > 36 Then Return Str(n)
|
|
Dim result As String = ""
|
|
Dim digit As Integer
|
|
While n > 0
|
|
digit = n Mod b
|
|
If digit < 10 Then
|
|
result = digit & result
|
|
Else
|
|
result = Chr(digit + 87) + result
|
|
End If
|
|
n \= b
|
|
Wend
|
|
Return result
|
|
End Function
|
|
|
|
Function convertToDecimal (s As Const String, b As UInteger) As UInteger
|
|
If b < 2 OrElse b > 36 Then Return 0
|
|
Dim t As String = LCase(s)
|
|
Dim result As UInteger = 0
|
|
Dim digit As Integer
|
|
Dim multiplier As Integer = 1
|
|
For i As Integer = Len(t) - 1 To 0 Step - 1
|
|
digit = -1
|
|
If t[i] >= 48 AndAlso t[i] <= min(57, 47 + b) Then
|
|
digit = t[i] - 48
|
|
ElseIf b > 10 AndAlso t[i] >= 97 AndAlso t[i] <= min(122, 87 + b) Then
|
|
digit = t[i] - 87
|
|
End If
|
|
If digit = -1 Then Return 0 '' invalid digit present
|
|
If digit > 0 Then result += multiplier * digit
|
|
multiplier *= b
|
|
Next
|
|
Return result
|
|
End Function
|
|
|
|
Dim s As String
|
|
|
|
For b As UInteger = 2 To 36
|
|
Print "36 base ";
|
|
Print Using "##"; b;
|
|
s = ConvertToBase(36, b)
|
|
Print " = "; s; Tab(21); " -> base ";
|
|
Print Using "##"; b;
|
|
Print " = "; convertToDecimal(s, b)
|
|
Next
|
|
|
|
Print
|
|
Print "Press any key to quit"
|
|
Sleep
|