Esempio n. 1
0
int				ft_atoi_base(const char *str, int base)
{
	int	i;
	int	sign;

	i = -1;
	sign = 1;
	if (base > 0 && base < 17)
	{
		if (!ft_strncmp(str, "0x", 2) || !ft_strncmp(str, "0X", 2))
			i += 2;
		while (str[++i])
		{
			if (ft_ishexa(str[i]))
				return (sign * ft_calc(str, i, base));
			if (str[i] == '-' || str[i] == '+')
			{
				if (!ft_ishexa(str[i + 1]))
					return (0);
				sign = 44 - str[i];
			}
			else if (!ft_is_space(str[i]))
				return (0);
		}
	}
	return (0);
}
Esempio n. 2
0
int			ft_atoi_more(const char *str)
{
	int		neg;
	long	nb;
	int		base;

	neg = 1;
	nb = 0;
	base = 10;
	while (*str == ' ' || (*str > 8 && *str < 14))
		str++;
	if (*str == '-' || *str == '+')
		neg = 44 - *str++;
	if (*str == '0' && (*str++))
	{
		if ((*str == 'x' || *str == 'X') && (*str++))
			base = 16;
		else if (*str == 'b' && (*str++))
			base = 2;
		else if (ft_isocta(*str))
			base = 8;
	}
	while ((base == 10 && ft_isdigit(*str)) || (base == 2 && ft_isbin(*str)) ||
			(base == 8 && ft_isocta(*str)) || (base == 16 && ft_ishexa(*str)))
		nb = nb * base + nb_atoi(*str++, base);
	return (neg * nb);
}
Esempio n. 3
0
int		ft_atoi_b(const char *str, int base)
{
	int			s;
	int			n;
	int			h;

	h = 0;
	s = 1;
	n = 0;
	if (str && base > 1 && base < 17)
	{
		while ((*str >= 9 && *str <= 13) || *str == ' ')
			str++;
		if (((s = (*str == '-' ? -1 : 1)) == -1 || *str == '+') && base == 10)
			str++;
		if (base == 16 && (h = ft_ishexa((char *)str)))
			str += 2;
		h -= 2;
		while (ft_isdigit(*str) || h-- > 0)
		{
			if (ft_isdigit(*str))
				n = n * base + (*str++ - '0');
			if (base > 10 && ft_isalpha(*str))
				n = n * base + (ft_tolower(*str++) - 'a' + 10);
		}
	}
	return (s * n);
}
Esempio n. 4
0
static int		ft_calc(const char *str, int i, int base)
{
	int		n;
	int		pow;
	int		tmp;

	n = 0;
	i -= 1;
	pow = -1;
	while (ft_ishexa(str[++i]))
		;
	while (ft_ishexa(str[--i]))
	{
		if ((tmp = ft_base(str[i], base)) == -1)
			break ;
		n += (ft_power(base, ++pow)) * tmp;
	}
	return (n);
}
Esempio n. 5
0
File: ft_xtoi.c Progetto: vlize/RT
intmax_t	ft_xtoi(char *x)
{
	intmax_t	n;
	int			i;

	if (!ft_ishexa(x[0]))
		return (-1);
	n = 0;
	i = -1;
	while (ft_ishexa(x[i += 1]))
	{
		n *= 16;
		if (ft_isdigit(x[i]))
			n += x[i] - 48;
		else if ((x[i] >= 'A') && (x[i] <= 'F'))
			n += x[i] - 55;
		else
			n += x[i] - 87;
	}
	return (n);
}
Esempio n. 6
0
static int		ft_convert_base(char *str)
{
    int		*nbrs;
    int		i;
    int		len;
    int		r_value;

    i = 0;
    r_value = 0;
    len = ft_hexalen((char *)str);
    nbrs = ft_nbrnew(len);
    while (str[i] && ft_ishexa(str[i]))
    {
        nbrs[i] = ft_hexa(str[i]) * ft_power(16, (len - 1) - i);
        i++;
    }
    i = 0;
    while (str[i] && ft_ishexa(str[i]))
    {
        r_value += nbrs[i];
        i++;
    }
    return (r_value);
}
Esempio n. 7
0
int				ft_atoi_hexa(const char *str)
{
    int		result;
    int		sign;

    sign = 1;
    result = 0;
    if (ft_strlen(str) == 0)
        return (0);
    if ((*str == '-' || *str == '+'))
    {
        (*str == '-') ? sign = -1 : 0;
        str++;
    }
    if (ft_ishexa(*str))
        result = ft_convert_base((char *)str);
    return (result * sign);
}