From 87af05c0cc20c8ee25504b37b65e9aaefc61bce5 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Thu, 20 Feb 2020 19:04:48 +0100 Subject: [PATCH] convertbase fonctionne bien --- srcs/ft_convertbase.c | 68 ++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/srcs/ft_convertbase.c b/srcs/ft_convertbase.c index a11fe5c..ca3ec66 100644 --- a/srcs/ft_convertbase.c +++ b/srcs/ft_convertbase.c @@ -1,23 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convertbase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/20 19:02:50 by hulamy #+# #+# */ +/* Updated: 2020/02/20 19:03:51 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + /* ** take a string that is a number in a certain and convert it in another base ** return the new string */ - - #include // for printf - #include // for atoi - - char *ft_convertbase(char *nbr, char *base_from, char *base_to); - - int main(int ac, char **av) - { - if (ac != 4) - printf("usage:\nchar *nbr, char *base_from, char *base_to\ntry the max long unsigned int : 9223372036854775807"); - else - printf("[%s]\n",ft_convertbase(av[1], av[2], av[3])); - return (0); - } - +/* +** #include // for printf +** #include // for atoi +** +** char *ft_convertbase(char *nbr, char *base_from, char *base_to); +** +** int main(int ac, char **av) +** { +** if (ac != 4) +** { +** printf("usage:\nchar *nbr, char *base_from, char *base_to\n"); +** printf("try the max long unsigned int : 18446744073709551615\n"); +** } +** else +** printf("[%s]\n",ft_convertbase(av[1], av[2], av[3])); +** return (0); +** } +*/ #include "libft.h" @@ -84,6 +99,8 @@ int ** -transform a nbr written as a string into a decimal nbr ** -it's an unsigned nbr because the negativity is managed elsewhere ** -if the number is bigger than the max unsigned long int it's false +** as it's impossible to verify if a number is bigger than the biggest +** unsigned, we verify the difference before the multiplication */ unsigned long int @@ -104,7 +121,7 @@ unsigned long int j = 0; while (nbr[i] != base[j] && base[j]) j++; - if ((18446744073709551615U - j) / length > decimal) + if ((18446744073709551615U - j) / length < decimal) return (0); decimal = (decimal * length) + j; i++; @@ -112,13 +129,20 @@ unsigned long int return (decimal); } +/* +** -it counts the size needed to be allocated +** -if the given nbr was a negative one it add the place for the '-' +** -then convert the nbr from decimal base to destination base +** into the string allocated +*/ + char *decimal_to_base(unsigned long int decimal, char *base, int malloc_size) { - int base_size; - int neg; - char *result; - unsigned long int nb; + int base_size; + int neg; + char *result; + unsigned long int nb; neg = malloc_size; base_size = 0; @@ -139,6 +163,10 @@ char return (result); } +/* +** main function to convert from one base to another +*/ + char *ft_convertbase(char *nbr, char *base_from, char *base_to) {