/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_convert_base2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: whaffman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/17 17:26:04 by whaffman #+# #+# */ /* Updated: 2024/06/19 18:51:15 by whaffman ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include int ft_isbase(char *str) { int i; int n; n = 0; while (str[n]) { i = 1; if (str[n] == '-' || str[n] == '+') return (0); while (str[n + i]) { if (str[n] == str[n + i]) return (0); i++; } n++; } return (n); } int ft_indexof(char c, char *str) { int result; result = 0; while (str[result]) { if (str[result] == c) return (result); result++; } return (-1); } int ft_atoi_base(char *str, char *base) { int sign; int res; int b; b = ft_isbase(base); while (ft_indexof(*str, " \f\n\r\t\v") >= 0) str++; res = 0; sign = 1; while (*str == '-' || *str == '+') { if (*str == '-') sign *= -1; str++; } while (ft_indexof(*str, base) >= 0) res = b * res + ft_indexof(*str++, base); return (sign * res); } void ft_write_str(char c, char *str) { int i; i = 0; while (str[i]) { i++; } str[i++] = c; str[i] = '\0'; } void ft_putnbr_base(int nbr, char *base, char *result) { int b; b = ft_isbase(base); if (b < 2) return ; if (nbr >= 0 && nbr < b) { ft_write_str(base[nbr], result); } else if (nbr < 0) { ft_write_str('-', result); if (nbr > -b) ft_putnbr_base(-nbr, base, result); else { ft_putnbr_base(nbr / (-b), base, result); ft_putnbr_base(-(nbr % b), base, result); } } else if (nbr >= b) { ft_putnbr_base(nbr / b, base, result); ft_putnbr_base(nbr % b, base, result); } }