[Gambas-user] Convert string to integer with arbitrary base

Caveat Gambas at ...1950...
Sat Jan 21 00:55:41 CET 2012


Hi Tobias

I'm sorry if I haven't completely understood your request (perhaps
you're asking Benoit for a function built in to Gambas?) but I have here
some code I wrote to help me do arbitrary conversions of a number in any
base to any other base (well up to base 36 anyway... then I ran out of
digits!).

It works with unsigned ints and simply returns -1 if a digit is out of
range e.g. trying to convert "3G" from hex.
There's 2 parts to it, toInt() and intToBase() which can be called
independently but I tend to just use convertBase() all the time.

It's probably not very efficient, but it works well enough for me and
has found its way into a few different projects of mine (I tend to do
quite a few things that require binary or hex conversions...).

Benoit will probably point us to some inbuilt function in Gambas that
makes all of this redundant but he can't take away the fun I had writing
it ;-)

Anyway, here's the code:

' Give some convenient names for the common bases
Public Const BASE_BINARY As Integer = 2
Public Const BASE_OCTAL As Integer = 8
Public Const BASE_DENARY As Integer = 10
Public Const BASE_TEN As Integer = 10
Public Const BASE_HEX As Integer = 16
' This table of digit values allows up to base36, using the same
' principle as for standard hex representation
' (i.e. A=10, B=11...F=15 etc)
Private DIGITS As String[] = ["0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", 
                              "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M",
                              "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z"]

Public Function convertBase(numberIn As String, fromBase As Integer,
toBase As Integer) As String

  Dim value As Integer
  value = toInt(numberIn, fromBase)
  Return intToBase(value, toBase)

End

Public Function intToBase(numberIn As Integer, base As Integer) As
String
  
  Dim remain, numToDivide As Integer
  Dim result As String = ""
  
  numToDivide = numberIn
  If numToDivide < 1 Then
    result = "0"
  Else
    Do While numToDivide / base > 0
      remain = numToDivide Mod base
      numToDivide = (Int)(numToDivide / base)
      result = DIGITS[remain] & result
    Loop
  End If
  Return result
  
End

Public Function toInt(inputStr As String, base As Integer) As Integer
  
  Dim idx, mult, result, value As Integer
  mult = 1
  For idx = Len(inputStr) To 1 Step -1
    ' If we're in a base with digits bigger than 9
    ' we need the Find to return 10 for A, 11 for B, 12 for C etc.
    value = DIGITS.Find(UCase(Mid$(inputStr, idx, 1)))
    ' If we have a digit out of range, return -1
    If value >= base Then
      Return -1
    Endif
    value = value * mult
    result = result + value
    mult = mult * base
  Next
  Return result
  
End

Kind regards,
Caveat


On Fri, 2012-01-20 at 23:17 +0100, tobias wrote:
> oops,
> make this comma in the for loop head a semicolon...
> 
> > /*
> >    * STR:          I string containing symbols
> >    * AL:           I alphabet to put symbols in order
> >    * RET:          O result in integer format
> >    * return value: O number of symbols put into the integer
> >    */
> > char stoi_al(char *str, char *al, unsigned int *ret)
> > {
> >       int i;
> >       unsigned int n = 0, base, c = 0;
> >       char *ind;
> >
> >       base = strlen(al);
> >       for (i = 0, str[i]; i++)
> >       {
> >           if (!(ind = strchr(al, str[i]))) break;
> >           n = n * base + ind - al;
> >           c++;
> >       }
> >       *ret = n;
> >       return c;
> > }
> >
> 
> 
> ------------------------------------------------------------------------------
> Keep Your Developer Skills Current with LearnDevNow!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-d2d
> _______________________________________________
> 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