[Gambas-user] Convert string to integer with arbitrary base
tobias
tobiasboe1 at ...20...
Fri Jan 20 22:39:16 CET 2012
hi,
i haven't found any function to convert a string to an integer with this
string containing signs of an arbitrary number base. won't it be useful?
this is a piece of code i once wrote for this purpose in c:
/*
* 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;
}
for i just needed it to work this unsigned integers it does just that
but can certainly be expanded (using '-' to negate will however imply
that '-' is not part of AL).
example for hex conversion would be:
stoi("ffffffff", "0123456789abcdef", &i);
printf("%u", i);
is this appreciated or didn't i recognise a function already existing in
gambas here? i haven't thought about any risks yet when playing around
with the alphabet...
regards,
tobi
More information about the User
mailing list