-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathft_utoa.c
More file actions
53 lines (48 loc) · 1.42 KB
/
ft_utoa.c
File metadata and controls
53 lines (48 loc) · 1.42 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_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlambert <rlambert@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/22 14:07:58 by rlambert #+# #+# */
/* Updated: 2015/02/26 14:21:05 by roblabla ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_countchar(unsigned int nbr, size_t base)
{
int i;
i = 0;
if (nbr == 0)
return (1);
else
{
while (nbr != 0)
{
nbr /= base;
i++;
}
return (i);
}
}
char *ft_utoa(unsigned int nbr, char const *base_chr)
{
char *buf;
int i;
size_t base;
base = ft_strlen(base_chr);
if ((buf = ft_strnew(ft_countchar(nbr, base))) == NULL)
return (NULL);
i = 0;
if (nbr == 0)
buf[i++] = '0';
while (nbr > 0)
{
buf[i++] = base_chr[nbr % base];
nbr /= base;
}
buf[i] = '\0';
ft_strrev(buf);
return (buf);
}