int ft_atoi_base(char *str, char *base) { int number; int base_nbr; int negatif; negatif = 1; if (!ft_check_base(base)) return (0); if (*str == '-' || *str == '+') { negatif = (*str == '-') ? -1 : 1; str++; } base_nbr = (int)ft_strlen(base); number = 0; while (*str != '\0') { number *= base_nbr; if (ft_find_base(*str, base) == -1) return (0); number += (ft_find_base(*str, base)); str++; } return (number * negatif); }
int ft_atoi_base(char *str, char *base) { int i; int s; int nbr; int nbr_final; int size_base; i = 0; s = 1; nbr_final = 0; size_base = 0; if (ft_check_str(str, base) && ft_check_base(base)) { while (((str[i] >= 9) && (str[i] <= 13)) || str[i] == 32) i++; if (str[i] == '-') { s = -1; i++; } else if (str[i] == '+') i++; nbr = get_nbr(str, base, i); while (base[size_base]) size_base++; i = 0; nbr *= 10; while ((nbr /= 10) > 0) nbr_final += (nbr % 10) * ft_power(size_base, i++); } return (nbr_final * s); }
int main (void) { char str[] = "-eqweq"; char base[] = "qwerty"; printf("%d", ft_check_str(str, base)); printf("\n"); printf("%d", ft_check_base(base)); printf("\n"); printf("%d\n", ft_atoi_base(str, base)); return (0); }
int ft_atoi_base(char *str, char *base) { int b; int result; int negative; result = 0; while (*str == '\v' || *str == '\t' || *str == '\f' || *str == '\r' || *str == '\n' || *str == ' ') str++; if ((negative = 0) || *str == '-' || *str == '+') negative = ((*str++ == '-') ? 1 : 0); if ((b = ft_check_base(base)) && ft_check_str(str, base)) while (*str++) result = ((b == 2 && !result) ? ft_get_rank(*(str - 1), base) : ((result * b) + ft_get_rank(*(str - 1), base))); return ((negative) ? -result : result); }
char *ft_ultoa_base(unsigned long nb, char *base) { unsigned long i; char *str; i = 0; if (!(str = ft_strnew(60)) || (ft_check_base(base) == 0)) return (NULL); if (ft_strlen_base(1, base, "a", 'a') < 2) return (str); if (nb == 0) { free(str); return (ft_strdup("0")); } while (nb > 0) { str[i] = base[nb % ft_strlen_base(1, base, "a", 'a')]; nb = nb / ft_strlen_base(1, base, "a", 'a'); i++; } return (ft_strrev(ft_strdupclr(str))); }