From d963161275bcb3af4097872ba033da3ff9255606 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 30 Oct 2019 03:57:47 +0100 Subject: Individual convert functions, more flags - Each conversion type is handle by individual functions in convert_* files - handle the ' ', '#', '+' and length modifier flags. --- utils.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index c018e5a..09b2bd5 100644 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/29 00:12:40 by cacharle #+# #+# */ -/* Updated: 2019/10/29 00:12:52 by cacharle ### ########.fr */ +/* Updated: 2019/10/30 03:53:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,3 +29,48 @@ int strrchr_index(const char *s, char c) } return (i); } + +static int nbrlen_radix(long int nbr, int radix) +{ + int counter; + long unsigned int u_nbr; + + if (nbr == 0) + return (1); + counter = 0; + u_nbr = nbr; + if (nbr < 0) + { + counter++; + u_nbr = -nbr; + } + while (u_nbr > 0) + { + u_nbr /= radix; + counter++; + } + return (counter); +} + +char *ft_itoa_base(long long int n, char *base) +{ + char *str; + int len; + int radix; + long long unsigned int u_nbr; + + radix = ft_strlen(base); + len = nbrlen_radix(n, radix); + if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + str[len] = '\0'; + u_nbr = n < 0 ? -n : n; + if (n < 0) + str[0] = '-'; + while (--len >= (n < 0 ? 1 : 0)) + { + str[len] = base[u_nbr % radix]; + u_nbr /= radix; + } + return (str); +} -- cgit