[Gambas-user] Base Conversion

Pablo Vera pvera at ...729...
Sun Nov 16 20:27:00 CET 2008


Here is a VB6 code to convert to and from base36, it should work almost 
the same in Gambas:


' Support functions

' Get a base36 digit
Private Function D36(ByVal N As Integer) As String
  D36 = Chr$(N + IIf(N <= 9, 48, 55))
End Function

' base36 Logarithm
Private Function Log36(ByVal X As Double) As Double
  Log36 = Log(X) / Log(36#)
End Function


' Convert a base10 number (Num10) to base36 with a maximum of RNum digits
'
Public Function ToBase36(ByVal Num10 As Long, ByVal RNum As Integer) As 
String
Dim Num36 As String, Base As Long, Mult As Integer, E As Integer
  Num36 = String$(RNum, "0")
 
  If Num10 > 0 Then
    E = Int(Log36(Num10))
   
    Do
      Base = 36 ^ E
      Mult = Int(Num10 / Base)
      Mid$(Num36, RNum - E, 1) = D36(Mult)
     
      Num10 = Num10 - Mult * Base
      E = E - 1
    Loop Until E < 0
  End If
 
  ToBase36 = Num36
End Function


' Convert a base36 number (Num36) back to base10
'
Public Function ToBase10(ByVal Num36 As String) As Long
Dim Num10 As Long, I As Integer, D As Long, C As String, E As Integer
  Num10 = 0
 
  E = Len(Num36)
  For I = E To 1 Step -1
    C = UCase$(MID$(Num36, I, 1))
    If C >= "A" And C <= "Z" Then
      D = ASc(C) - 55
    ElseIf C >= "0" And C <= "9" Then
      D = ASc(C) - 48
    Else
      D = 0
    End If
    Num10 = Num10 + D * (36 ^ (E - I))
  Next I
 
  ToBase10 = Num10
End Function

Pablo
_____________________________________________________________________



Jason Hackney wrote:
> A cursory look through the docs didn't turn up anything... Is there a
> function to convert base, say base 10 to base 36? Or base 16 to base 36. I
> want to get to base 36 from either base10 or base16.
>
> It shouldn't be too difficult to hack something together, but was wondering
> if I'm overlooking something that's already there.
>
> Thanks!
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>   




More information about the User mailing list