-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_convert_base.c
119 lines (110 loc) · 2.42 KB
/
ft_convert_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chrhuang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 10:33:30 by chrhuang #+# #+# */
/* Updated: 2018/11/21 10:21:56 by chrhuang ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int check_base(char *base)
{
int i;
int j;
j = 0;
if (base == NULL)
return (-1);
while (base[j] != '\0')
{
i = j + 1;
while (base[i] != '\0')
{
if (base[j] == base[i])
return (-1);
i = i + 1;
}
j = j + 1;
}
if (j == 0 || j == 1)
return (-1);
return (j);
}
static int check_nbr_with_base(char *nbr, char *base)
{
int i;
int j;
int tmp;
j = 0;
while (nbr[j] != '\0')
{
i = 0;
tmp = 0;
while (base[i] != '\0')
{
if (nbr[j] == base[i])
{
tmp = 1;
break ;
}
i = i + 1;
}
if (tmp == 0)
return (-1);
j = j + 1;
}
return (0);
}
static void fill_res(int nb, char *base, int len, char *res)
{
int i;
int j;
char tmp;
i = 0;
while (nb != 0)
{
res[i] = base[nb % len];
nb = nb / len;
i = i + 1;
}
res[i] = '\0';
i = i - 1;
j = 0;
while (i > j)
{
tmp = res[i];
res[i] = res[j];
res[j] = tmp;
i = i - 1;
j = j + 1;
}
}
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
{
int len1;
int len2;
int res_tmp;
char *res;
if (nbr == NULL || base_from == NULL || base_to == NULL)
return (NULL);
len1 = check_base(base_from);
len2 = check_base(base_to);
if (check_nbr_with_base(nbr, base_from) == -1)
return (NULL);
if (len1 == -1 || len2 == -1)
return (NULL);
res_tmp = ft_atoi_base(nbr, base_from);
if ((res = malloc(sizeof(char) * (len1 * len2))) == NULL)
return (NULL);
if (res_tmp == 0)
{
res[0] = '0';
res[1] = '\0';
return (res);
}
fill_res(res_tmp, base_to, len2, res);
return (res);
}