-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_convert_base_unsigned.c
More file actions
113 lines (102 loc) · 2.59 KB
/
Copy pathft_convert_base_unsigned.c
File metadata and controls
113 lines (102 loc) · 2.59 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmelvin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/14 17:44:39 by tmelvin #+# #+# */
/* Updated: 2019/12/17 20:57:03 by tmelvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int base_is_invalid(char *base, int base_nbr)
{
int i;
int j;
if (base_nbr <= 1)
return (1);
i = 0;
while (base[i])
{
if (base[i] == '+' || base[i] == '-')
return (1);
else if (base[i] < 33)
return (1);
j = i + 1;
while (base[j])
{
if (base[j] == base[i])
return (1);
j++;
}
i++;
}
return (0);
}
static int find_char(char c, char *base_from)
{
int i;
i = 0;
while (base_from[i])
{
if (base_from[i] == c)
return (i);
i++;
}
return (-1);
}
static unsigned long long debase(char *nbr, char *base_from,
int base_from_nbr)
{
unsigned long long result;
result = 0;
while (*nbr < 33)
nbr++;
while (*nbr && find_char(*nbr, base_from) > -1)
{
result = result * base_from_nbr;
result = result + find_char(*nbr, base_from);
nbr++;
}
return (result);
}
static char *rebase(unsigned long long nb, char *base_to,
int base_to_nbr)
{
int i;
char *output;
i = 0;
output = malloc(65 * sizeof(char));
while (nb > 0)
{
output[i++] = base_to[nb % base_to_nbr];
nb /= base_to_nbr;
}
output[i] = '\0';
ft_strrev(output);
return (output);
}
char *ft_convert_base_unsigned(char *nbr,
char *base_from, char *base_to)
{
unsigned long long debased_nbr;
int base_from_nbr;
int base_to_nbr;
char *output;
base_from_nbr = ft_strlen(base_from);
base_to_nbr = ft_strlen(base_to);
if (base_is_invalid(base_from, base_from_nbr) ||
base_is_invalid(base_to, base_to_nbr))
return (NULL);
debased_nbr = debase(nbr, base_from, base_from_nbr);
if (debased_nbr == 0)
{
output = malloc(2 * sizeof(char));
output[0] = base_to[0];
output[1] = '\0';
}
else
output = rebase(debased_nbr, base_to, base_to_nbr);
return (output);
}