-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_aux_pf.c
84 lines (73 loc) · 1.89 KB
/
ft_aux_pf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_aux_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adiaz-be <adiaz-be@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/15 11:35:45 by adiaz-be #+# #+# */
/* Updated: 2022/10/15 11:36:22 by adiaz-be ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen_pf(const char *str)
{
size_t c;
if (!str)
return (0);
c = 0;
while (str[c])
c++;
return (c);
}
void ft_bzero_pf(void *s, size_t n)
{
unsigned char *ptr;
ptr = (unsigned char *)s;
while (n > 0)
{
*ptr = 0;
ptr++;
n--;
}
}
void *ft_calloc_pf(size_t number, size_t size)
{
void *dest;
dest = malloc (number * size);
if (dest == NULL)
return (NULL);
ft_bzero_pf(dest, number * size);
return (dest);
}
static size_t ft_len(unsigned long long n, char *base)
{
size_t len;
unsigned long long base_len;
len = 1;
base_len = ft_strlen_pf(base);
while (n >= base_len)
{
n /= base_len;
len++;
}
return (len);
}
char *ft_aux_pf(unsigned long long n, char *base)
{
char *str;
int num_len;
int base_len;
num_len = ft_len(n, base);
base_len = ft_strlen_pf(base);
str = ft_calloc_pf((num_len + 1), sizeof(char));
if (!str)
return (NULL);
while (num_len)
{
num_len = num_len - 1;
str[num_len] = base[n % base_len];
n /= base_len;
}
return (str);
}