-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_base.c
56 lines (50 loc) · 1.51 KB
/
ft_atoi_base.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nvienot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/21 15:02:50 by nvienot #+# #+# */
/* Updated: 2019/03/24 16:59:33 by nvienot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// attention || fin de ligne
static int ft_base(int nb, int base)
{
char *base1;
char *base2;
int i;
base1 = "0123456789abcdef";
base2 = "0123456789ABCDEF";
i = 0;
while (i < base)
{
if (nb == base1[i] || nb == base2[i])
return (i);
i++;
}
return (-1);
}
int ft_atoi_base(const char *str, int base)
{
int nb;
int neg;
int i;
nb = 0;
neg = 0;
i = 0;
while (((str[i] >= 8 && str[i] <= 13) || str[i] == ' ') && str[i])
i++;
if (str[i] == '-')
neg++;
if (str[i] == '-' || str[i] == '+')
i++;
while (ft_ishexa(str[i]))
{
nb = nb * base + (ft_base(str[i], base));
i++;
}
return (neg ? -nb : nb);
}