Esempio n. 1
0
char			*ft_itoa(long n)
{
	char	*a_nb;
	int		neg;
	int		i;

	neg = 1;
	if (!(a_nb = malloc((ft_nbdigits(n) + 1) * sizeof(char))))
		return (NULL);
	if (n == 0 || n < -9223372036854775807)
		return (ft_except(n, &a_nb));
	if (n < 0)
	{
		neg = -1;
		n *= -1;
	}
	i = 0;
	while (n > 0)
	{
		a_nb[i++] = '0' + (n % 10);
		n /= 10;
	}
	if (neg == -1)
		a_nb[i++] = '-';
	a_nb[i] = '\0';
	return (ft_strrev(a_nb));
}
Esempio n. 2
0
char			*ft_itoa(int n)
{
	char		*str;
	size_t		countn;
	int			i;

	i = 0;
	countn = 0;
	str = NULL;
	if (n == -2147483648)
		return (str = ft_except(str));
	if (n < 0)
	{
		n = n * -1;
		i = 1;
	}
	countn = ft_countn(n) + i;
	if ((str = ft_strnew(countn + 1)) == NULL)
		return (NULL);
	ft_check(str, n, countn);
	if (i == 1)
		str[0] = '-';
	return (str);
}