forked from ruv1nce/42-exam_beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi_base.c
More file actions
42 lines (38 loc) · 755 Bytes
/
Copy pathft_atoi_base.c
File metadata and controls
42 lines (38 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
int ft_value(char c, int base)
{
if ((base <= 10 && c >= '0' && c <= '0' + base - 1) || (base >= 11 && c >= '0' && c <= '9'))
return (c - '0');
else if (base >= 11 && c >= 'a' && c <= 'a' + base - 11)
return (c - 87);
else if (base >= 11 && c >= 'A' && c <= 'A' + base - 11)
return (c - 55);
else
return (-1);
}
int ft_atoi_base(const char *str, int str_base)
{
int posneg;
int res;
res = 0;
while (*str <= ' ')
str++;
if (*str == '+')
str++;
else if (*str == '-')
{
posneg = -1;
str++;
}
while (ft_value(*str, str_base) != -1)
{
res = res * str_base + ft_value(*str, str_base);
str++;
}
return (res * posneg);
}
int main(void)
{
char *s = " -1233afby";
printf("%i\n", ft_atoi_base(s, 4));
}