43 lines
1.3 KiB
C
43 lines
1.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* :::::::: */
|
|
/* ft_ltoa.c :+: :+: :+: */
|
|
/* +:+ */
|
|
/* By: djonker <marvin@codam.nl> +#+ */
|
|
/* +#+ */
|
|
/* Created: 2020/11/12 23:20:24 by djonker #+# #+# */
|
|
/* Updated: 2023/02/07 00:40:35 by houtworm ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../libft.h"
|
|
|
|
char *ft_ltoa(long long n)
|
|
{
|
|
char *r;
|
|
int l;
|
|
|
|
l = ft_intlen(n) - 1;
|
|
r = ft_calloc(l + 2, 1);
|
|
if (r == NULL)
|
|
return (NULL);
|
|
while ((n > 9 || n < 0) && n >= -9223372036854775807)
|
|
{
|
|
if (n >= 10)
|
|
{
|
|
r[l] = n % 10 + '0';
|
|
l--;
|
|
n = (n / 10);
|
|
}
|
|
else
|
|
{
|
|
r[0] = '-';
|
|
n = (n * -1);
|
|
}
|
|
}
|
|
r[l] = n + '0';
|
|
if (n < -9223372036854775807)
|
|
ft_strlcpy(r, "-9223372036854775808", 20);
|
|
return (r);
|
|
}
|