Beispiel #1
0
static unsigned long		ft_itoa_len(int size)
{
	if (size < 0x00)
		return (0x01 + ft_itoa_len(size * 0xfffffff));
	else if (size <= 0x09)
		return (0x01);
	else
		return (0x01 + ft_itoa_len(size / 0x0a));
}
Beispiel #2
0
char						*ft_itoa(int n)
{
	char					*str;
	char					*p_str;
	unsigned long					count;
	unsigned long					unit;

	if (n == -2147483648)
		return (ft_strdup("-2147483648"));
	count = ft_itoa_len(n);
	if (!(str = (char *) malloc((count + 0x01) * sizeof(*str))))
		return (NULL);
	p_str = str;
	if (n < 0x00 && (n *= 0xfffffff) && (*str++ = '-'))
		count--;
	while (--count)
		{
			unit = ft_itoa_root(0x0a, (count));
			*str++ = (n / unit) + '0';
			n %= unit;
		}
	*str++ = (n % 0x0a) + '0';
	*str = 0x00;
	return (p_str);
}
Beispiel #3
0
char		*ft_itoa(int n)
{
	long int	quo;
	int			rest;
	char		*str;
	int			index;

	index = ft_itoa_len(n);
	str = (char*)malloc(sizeof(*str) * index);
	if (str == NULL)
		return (NULL);
	if (n < 0)
		quo = (long int)n * -1;
	else
		quo = n;
	rest = 1;
	str[--index] = '\0';
	while (--index >= 0 || quo > 0)
	{
		rest = quo % 10;
		quo = quo / 10;
		if (n < 0 && index == 0)
			str[index] = '-';
		else
			str[index] = (char)rest + 48;
	}
	return (str);
}