0

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);
}
Eric Postpischil
  • 168,892
  • 12
  • 149
  • 276
hugo hg
  • 21
  • 4
  • Signed integer overflow is undefined behavior, and that's exactly what will happen when you do `0x8000000 * 16`. – Nate Eldredge Jul 13 '21 at 04:42
  • So why this is not working when I change his type to long ? – hugo hg Jul 13 '21 at 05:06
  • 1
    Whose type? Are you sure that `long` on your system is actually larger? – Nate Eldredge Jul 13 '21 at 05:14
  • Im sure, yes ! I also tried long long but still not working – hugo hg Jul 13 '21 at 05:18
  • `check_pos` returns the value digit read. So what does the test `while (check_pos(base, str[i]) && str[i])` do when the digit is `'0'`? – Nate Eldredge Jul 13 '21 at 05:19
  • 2
    By the way, this would be a great opportunity to use (or learn to use) a debugger. This made it extremely easy for me to find the bug. You could have saved yourself at least an hour of writing the question, waiting for an answer, etc. – Nate Eldredge Jul 13 '21 at 05:20
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714), [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – phuclv Jul 13 '21 at 07:18
  • `itoa()` should not be used in production code. It provides no error checking at all. it will happily accept `atoi("my cow");` and silently return `0` without any indication of a failure... Instead, at a minimum use `sscanf()` or for full error reporting use `strtol()`. – David C. Rankin Jul 13 '21 at 07:43
  • 1
    @DavidC.Rankin: The code contains neither `itoa` nor `atoi`. The question states it is trying to make an `itoa` **variant**, although that appears to be a typo and the intent is to make an `atoi` variant, and the variant code shown returns an error indication. So a knee-jerk response to seeing the letters `itoa` is inappropriate. – Eric Postpischil Jul 13 '21 at 11:20
  • To return a product of `INT_MIN` from `res * -1` can only happen due to _undefined behavior_. Code needs a new approach. – chux - Reinstate Monica Jul 13 '21 at 13:32
  • @EricPostpischil the comment was in response to `"Return int min value with itoa in c"`. Let's not go out of our way to be condescending. – David C. Rankin Jul 14 '21 at 01:20
  • @DavidC.Rankin: My comment stands, with the exception that I was mistaken about OP’s variant returning an error indication. They are using neither `itoa` nor `atoi`, and your comment was a knee-jerk reaction made without understanding the question. – Eric Postpischil Jul 14 '21 at 01:49

0 Answers0