-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_ultob.c
More file actions
53 lines (48 loc) · 1.55 KB
/
ft_printf_ultob.c
File metadata and controls
53 lines (48 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_converters.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbiasuz <lbiasuz@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/12 16:18:27 by lbiasuz #+# #+# */
/* Updated: 2023/01/31 09:54:42 by lbiasuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static unsigned int long_long_size(unsigned long long nb, int base_len)
{
unsigned int i;
i = 1;
if (nb <= 0)
i++;
while (nb)
{
i++;
nb = nb / base_len;
}
return (i);
}
char *ft_ultob(unsigned long long nb, char *base)
{
char *output;
int base_len;
int size;
if (!nb)
return (ft_strdup("(nil)"));
base_len = ft_strlen(base);
size = long_long_size(nb, base_len);
output = (char *) malloc(sizeof(char) * (size + 2));
size = size + 2;
if (!output)
return (NULL);
output[--size] = '\0';
while (size && nb)
{
output[--size] = base[nb % base_len];
nb = nb / base_len;
}
output[--size] = 'x';
output[--size] = '0';
return (output);
}