#include "my_atoi_base.h" int val_in_base(char c, const char *base) { size_t i; for (i = 0; base[i] && base[i] != c; i++) { continue; } if (base[i]) { return i; } return -1; } int base_size(const char *base) { int res; for (res = 0; base[res]; res++) { continue; } return res; } int my_atoi_base(const char *str, const char *base) { int res = 0; // str error check if (str == NULL || *str == '0') { return 0; } // trim whitespaces for (; *str && *str == ' '; str++) { continue; } // move to end of str size_t l; for (l = 0; str[l]; l++) { continue; } l--; // prepare for calculations int b = base_size(base); int factor = 1; // actual conversion of up to the second element of str (potential sign) for (; l > 0; l--) { int val = val_in_base(str[l], base); if (val == -1) { return 0; } res += val * factor; factor *= b; } // l should be 0 by now if (str[l] == '-') { return -res; } else if (str[l] != '+') { int val = val_in_base(str[l], base); if (val == -1) { return 0; } return res + val * factor; } return res; }