Originally Posted by Nero_3D
pawn Код:
stock BaseToInt(string[], const base) { if(string[0] && (1 < base < 37)) { new res = 0, cur = 1, i = strlen(string); for( ; (--i) != -1; cur *= base) switch(string[i]) { case '0'..'9': res += (cur * (string[i] - '0')); case 'a'..'z': res += (cur * (string[i] - 'a' + 10)); case 'A'..'Z': res += (cur * (string[i] - 'A' + 10)); default: return 0; } return res; } return 0; } #define DualToInt(%0) BaseToInt(%0, 2) #define OctalToInt(%0) BaseToInt(%0, 8) #define HexToInt(%0) BaseToInt(%0, 16)
pawn Код:
stock IntToBase(number, const base) { new str[32]; if(1 < base < 37) { new m = 1, depth = 0; while (m <= number) { m *= base; depth++; } for ( ; (--depth) != -1; ) { str[depth] = (number % base); number = ((number - str[depth]) / base); if(str[depth] > 9) str[depth] += 'A'; else str[depth] += '0'; } } return str; } #define IntToDual(%0) IntToBase(%0, 2) #define IntToOctal(%0) IntToBase(%0, 8) #define IntToHex(%0) IntToBase(%0, 16)
|