-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_utoa_base.c
More file actions
47 lines (42 loc) · 1.39 KB
/
ft_utoa_base.c
File metadata and controls
47 lines (42 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mfrisby <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/07/11 13:19:19 by mfrisby #+# #+# */
/* Updated: 2017/07/11 13:19:21 by mfrisby ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static int ft_unblen_base(unsigned long long n, int base)
{
int len;
long double pow;
pow = 1;
len = 0;
while (n >= (pow *= base))
len++;
return (len + 1);
}
char *ft_utoa_base(unsigned long long nb, int base)
{
char *s;
int len;
len = ft_unblen_base(nb, base);
if (!(s = ft_strnew(len)))
return (NULL);
if (nb == 0)
{
s[0] = '0';
s[1] = 0;
return (s);
}
while (nb)
{
s[--len] = nb % base > 9 ? nb % base - 10 + 'a' : nb % base + '0';
nb /= base;
}
return (s);
}