-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ft_atoi_base condition should exclude max_digit #67
Comments
create a pull request and I'll accept the request. |
Had the same kind of problem with 'f' being accepted in base 15. |
create a pull request and I'll update it. Thanks! |
longer but: from issue pasqualerossi#67 : "In the line 15 and line 17, the condition is wrong. In any base b, the digits must be in the range of 0 to b -1 . For example, the valid digits are 0 and 1 if we are talking about base 2. So the condition should exceed the maximum allowable digits. " we can fix the in 2 ways I think, adding (-1) or excluding max_digit in the condition... I just added -1 to let the exercise be kinda clear...idk: in any case this is another version of int get_digit (char c, int digits_in_base); ------------------------ int get_digit(char c, int digits_in_base) { if (digits_in_base <= 10) { if (c >= '0' && c < '0' + digits_in_base) return (c - '0'); } else { if (c >= '0' && c <= '9') return (c - '0'); else if (c >= 'a' && c < 'a' + digits_in_base - 10) return (10 + c - 'a'); } return (-1); } ------------------------- & also: 1. fixed the main loop condition: the condition while ((digit = get_digit(to_lower(*str), str_base)) >= 0) directly gets the digit and breaks the loop if it's invalid - this ensures that we handle characters properly 2. added checks 4 Input Validation: - Checks if the str is NULL. - Checks if str_base is within the valid range (2 to 16) - according to subject :)
In the line 15 and line 17, the condition is wrong. In any base$b$ , the digits must be in the range of 0 to $b-1$ . For example, the valid digits are 0 and 1 if we are talking about base 2. So the condition should exceed the maximum allowable digits.
The text was updated successfully, but these errors were encountered: