Public Function ConvertBases(ByVal s As String, ByVal FromBase As Integer, ByVal ToBase As Integer) As String
' Convert number in string representation in fromBase into toBase. Return result as a string
' Return error if input is empty
If String.IsNullOrEmpty(s) Then Return ""
' only do base 2 to base 36 (digit represented by charecaters 0-Z)"
If (FromBase < 2 Or FromBase > 36) Or (ToBase < 2 Or ToBase > 36) Then Return ""
s = s.ToUpper ' Convert to uppercase
Const Allowed As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If System.Text.RegularExpressions.Regex.IsMatch(s, "^[" & Allowed.Substring(0, FromBase) & "]$*") = False Then Return ""
' convert string to an array of integer digits representing number in frombase
Dim il As Integer = s.Length : Dim fs(il) As Integer
For i As Integer = s.Length - 1 To 0 Step -1 : fs(s.Length - (i + 1)) = Allowed.IndexOf(s(i)) : Next
Dim ol = il * (FromBase / ToBase + 1) ' find how many digits the output needs
Dim acc(ol + 10) As Integer ' assign accumulation array
Dim Result(ol + 10) As Integer ' assign the result array
acc(0) = 1 ' initialise first acculamtion array element with number 1
Dim ip As Integer = 0
' for each input digit
For i As Integer = 0 To il
For j As Integer = 0 To ol ' add the input digit times (fromBase^i in baseTo) to the output accumulator
Result(j) += acc(j) * fs(i) : ip = j
Do While (Result(ip) >= ToBase) ' fix & cascade any which exceeds toBase
Result(ip + 1) += Result(ip) \ ToBase : Result(ip) = Result(ip) Mod ToBase : ip += 1
Loop
Next
' Calculate the next power from^i) in toBase format
For j As Integer = 0 To ol : acc(j) *= FromBase : Next
ip = 0 : Do While acc(ip) >= ToBase 'check for any which exceed toBase
acc(ip + 1) += acc(ip) \ ToBase : acc(ip) = acc(ip) Mod ToBase : ip += 1
Loop
Next
' convert the output to string format (digits 0,toBase-1 converted to 0-Z characters)
ConvertBases = String.Empty ' initialise output string
ip = ol : While Result(ip) = 0 : ip -= 1 : End While
While ip >= 0 : ConvertBases &= Allowed(Result(ip)) : ip -= 1 : End While
If String.IsNullOrEmpty(ConvertBases) Then Return "0" ' //input was zero, return 0
' return the converted string
Return ConvertBases
End Function