I'm trying to make an atoi version with base in arguments, like that I can put number in which base I want and get the returned in INT var in base10.
So I'm trying to get INT MAX VALUE and that it works fine ! But that's not the same with INT MIN VALUE. It returned me a wrong value. The returned value is -8. But the rest is work finely ! Can you explain me why please. Thanks in advance !
int ft_strlen(char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}
int check_base(char *s1)
{
int i;
int j;
i = 0;
j = 0;
if (ft_strlen(s1) < 2)
return (-1);
while (s1[i])
{
if ((s1[i] >= 9 && s1[i] <= 13) || (s1[i] == ' '
|| s1[i] == '+' || s1[i] == '-'))
i++;
j = i + 1;
while (s1[j])
{
if (s1[i] == s1[j])
return (0);
j++;
}
i++;
}
return (1);
}
int check_pos(char *s, char c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == c)
return (i);
i++;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int i;
int size_base;
int res;
int sign;
res = 0;
i = 0;
sign = 1;
size_base = ft_strlen(base);
if (!check_base(base))
return (0);
while ((str[i] >= 9 && str[i] <= 13) || str[i] == ' ')
i++;
while (check_pos(base, str[i]) && str[i])
{
if (str[i] == '-')
sign = -sign;
else if (str[i] != '+'
&& str[i] >= 31 && str[i] <= 126)
res = (res * size_base) + check_pos(base, str[i]);
else
return (0);
i++;
}
return (res * sign);
}
int main(int ac, char **av)
{
printf("%d\n", ft_atoi_base("7FFFFFFF", "0123456789ABCDEF"));
printf("%d\n", ft_atoi_base("-80000000", "0123456789ABCDEF"));
return (0);
}