Code:
// Valid radix can be anything from 2 to 36.
#define Binary 2
#define Ternary 3
#define Trinary 3
#define Quintal 5
#define Oct 8
#define Octal 8
#define Dec 10
#define Decimal 10
#define Duodecimal 12
#define Dozenal 12
#define Hex 16
#define Hexadecimal 16
new ___key[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define ___ctod(%0) ('0' <= %0 <= '9' ? %0 - '0' : 'A' <= %0 <= 'Z' ? %0 -'A' + 10 : 'a' <= %0 <= 'z' ? %0 -'a' + 10 : 0)
// Converts an integer to a string.
stock itoa(val, out[], len, radix = Decimal) {
if ( ! val ) { out[0] = '0'; out[1] = 0; return; }
if ( ! ( 2 <= radix <= 36 ) ) { out[0] = '0'; out[1] = 0; return; }
for ( new i = len ; i ; i-- ) { out[i] = 0; }
for ( new i = len - 1 ; val && i > -1 ; --i, val /= radix )
out[len-i-1] = ___key[val % radix];
new len2 = strlen(out);
new temp;
for ( new i = 0 ; i < len2 / 2 ; i++ ) {
temp = out[i];
out[i] = out[len2-i-1];
out[len2-i-1] = temp;
}
}
// Converts a string to an integer.
stock atoi(string[], radix = Decimal) {
if ( ! ( 2 <= radix <= 36 ) ) { return 0; }
new result, mult = 1;
for ( new i = strlen(string) - 1 ; i > - 1 ; i-- ) {
result += ___ctod(string[i]) * mult;
mult *= radix
}
return result;
}