42_ft_printf/ft_ltoabase.c

39 lines
1.2 KiB
C
Raw Normal View History

2022-10-06 20:30:03 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-10 13:33:47 -04:00
/* ft_ltoabase.c :+: :+: :+: */
2022-10-06 20:30:03 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2022-10-10 13:33:47 -04:00
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:41:28 by cchauvet ### ########.fr */
2022-10-06 20:30:03 -04:00
/* */
/* ************************************************************************** */
2022-10-10 13:33:47 -04:00
#include "printf.h"
2022-10-06 20:30:03 -04:00
2022-10-10 13:33:47 -04:00
char *ft_ltoabase(long int n, char *base)
2022-10-06 20:30:03 -04:00
{
2022-10-10 13:33:47 -04:00
char *mid_out;
2022-10-06 20:30:03 -04:00
char *out;
2022-10-10 13:33:47 -04:00
unsigned long nb;
2022-10-06 20:30:03 -04:00
2022-10-10 13:33:47 -04:00
if (base == NULL)
return (NULL);
2022-10-06 20:30:03 -04:00
if (n < 0)
{
2022-10-10 13:33:47 -04:00
nb = -n;
mid_out = ft_ultoabase(nb, base);
if (mid_out == NULL)
return (NULL);
out = ft_strjoin("-", mid_out);
free(mid_out);
2022-10-06 20:30:03 -04:00
}
else
{
2022-10-10 13:33:47 -04:00
nb = n;
return (ft_ultoabase(nb, base));
2022-10-06 20:30:03 -04:00
}
return (out);
}