Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: improved example) |
(Added FreeBASIC) |
||
Line 988: | Line 988: | ||
END PROGRAM</lang> |
END PROGRAM</lang> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>' 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</lang> |
|||
{{out}} |
|||
<pre> |
|||
36 base 2 = 100100 -> base 2 = 36 |
|||
36 base 3 = 1100 -> base 3 = 36 |
|||
36 base 4 = 210 -> base 4 = 36 |
|||
36 base 5 = 121 -> base 5 = 36 |
|||
36 base 6 = 100 -> base 6 = 36 |
|||
36 base 7 = 51 -> base 7 = 36 |
|||
36 base 8 = 44 -> base 8 = 36 |
|||
36 base 9 = 40 -> base 9 = 36 |
|||
36 base 10 = 36 -> base 10 = 36 |
|||
36 base 11 = 33 -> base 11 = 36 |
|||
36 base 12 = 30 -> base 12 = 36 |
|||
36 base 13 = 2a -> base 13 = 36 |
|||
36 base 14 = 28 -> base 14 = 36 |
|||
36 base 15 = 26 -> base 15 = 36 |
|||
36 base 16 = 24 -> base 16 = 36 |
|||
36 base 17 = 22 -> base 17 = 36 |
|||
36 base 18 = 20 -> base 18 = 36 |
|||
36 base 19 = 1h -> base 19 = 36 |
|||
36 base 20 = 1g -> base 20 = 36 |
|||
36 base 21 = 1f -> base 21 = 36 |
|||
36 base 22 = 1e -> base 22 = 36 |
|||
36 base 23 = 1d -> base 23 = 36 |
|||
36 base 24 = 1c -> base 24 = 36 |
|||
36 base 25 = 1b -> base 25 = 36 |
|||
36 base 26 = 1a -> base 26 = 36 |
|||
36 base 27 = 19 -> base 27 = 36 |
|||
36 base 28 = 18 -> base 28 = 36 |
|||
36 base 29 = 17 -> base 29 = 36 |
|||
36 base 30 = 16 -> base 30 = 36 |
|||
36 base 31 = 15 -> base 31 = 36 |
|||
36 base 32 = 14 -> base 32 = 36 |
|||
36 base 33 = 13 -> base 33 = 36 |
|||
36 base 34 = 12 -> base 34 = 36 |
|||
36 base 35 = 11 -> base 35 = 36 |
|||
36 base 36 = 10 -> base 36 = 36 |
|||
</pre> |
|||
=={{header|FunL}}== |
=={{header|FunL}}== |