Before i go off and re-invent the wheel…

Does anyone have any xojo code to convert decimal to base 36 and back again?

Before i go off and re-invent the wheel…

Does anyone have any xojo code to convert decimal to base 36 and back again?

Theres VB code on wikipedia which should be darn near “cut & paste” that converts TO base36

The conversion back shouldnt be much more work

Cheers.

I will take a look.

These functions convert to and from base-10 and any base between 2 and 36:

```
Function ConvertFromBase(Value As String, FromBase As Integer) As Integer
' Converts the value into base-10 using FromBase
Static base() As String = Split("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
If FromBase - 1 > UBound(base) Or FromBase < 2 Or FromBase > 36 Then Raise New OutOfBoundsException
Dim Result As Integer
For i As Integer = 1 To Value.Len
Dim datum As Integer = Base.IndexOf(Value.Mid(i, 1))
Result = FromBase * Result + datum
Next
Return Result
End Function
```

```
Function ConvertToBase(Value As Integer, NewBase As Integer) As String
' Converts an Integer into a string representation of the number in NewBase
Static base() As String = Split("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
If NewBase - 1 > UBound(base) Or NewBase < 2 Or NewBase > 36 Then Raise New OutOfBoundsException
Dim digit() As String
Do Until Value = 0
Dim remainder As Integer = Value Mod NewBase
Value = Value \\ NewBase
digit.Insert(0, base(remainder))
Loop
Return Join(digit, "")
End Function
```

brilliant!

Thanks @Andrew Lambert !

Forum for Xojo Programming Language and IDE. Copyright © 2024 Xojo, Inc.