-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_convert_base.c
61 lines (55 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gantonio <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/14 18:19:52 by gantonio #+# #+# */
/* Updated: 2021/07/18 16:50:48 by gantonio ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_base(unsigned long long num, int base, int counter, char *str)
{
while (num != 0)
{
if ((num % base) < 10)
str[counter - 1] = (num % base) + 48;
else
str[counter - 1] = (num % base) + 55;
num /= base;
counter--;
}
return (str);
}
char *ft_convert_base(unsigned long long number, int base)
{
unsigned long long temp;
int counter;
char *str;
temp = number;
str = 0;
counter = 0;
if (number == 0)
return (ft_strdup("0"));
while (number != 0)
{
number /= base;
counter++;
}
str = malloc(counter + 1);
if (!str)
return (0);
str[counter] = '\0';
str = ft_base(temp, base, counter, str);
return (str);
}
char *ft_hex_tolower(char *str)
{
int i;
i = -1;
while (str[++i])
str[i] = ft_tolower(str[i]);
return (str);
}