This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_base.c
64 lines (58 loc) · 1.6 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
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: drestles <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/11 11:44:50 by gkessler #+# #+# */
/* Updated: 2019/02/17 18:08:14 by drestles ### ########.fr */
/* */
/* ************************************************************************** */
#include "rtv1.h"
#include "libft.h"
static int is_spaces(char c)
{
if (c == ' ' || c == '\n' || c == '\t' || c == '\v'
|| c == '\r' || c == '\f')
return (1);
return (0);
}
static int is_base(int c, int base)
{
char *str;
char *str_up;
int i;
str = "0123456789abcdef";
str_up = "0123456789ABCDEF";
i = 0;
while (i < base)
{
if (c == str[i] || c == str_up[i])
return (i);
i++;
}
return (-1);
}
int ft_atoi_base(char *str, int base)
{
int i;
int n;
int ng;
i = 0;
n = 0;
ng = 0;
while (is_spaces(str[i]))
i++;
if (str[i] == '0' && str[i + 1] == 'x')
i += 2;
while (is_base(str[i], base) != -1)
{
n = n * base;
n = n + is_base(str[i], base);
i++;
}
if (ng)
return (-n);
return (n);
}