-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_utoa_base.c
executable file
·38 lines (35 loc) · 1.44 KB
/
ft_utoa_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/17 06:29:21 by akharrou #+# #+# */
/* Updated: 2019/04/23 18:58:51 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Includes/stdlib_42.h"
#include "../Includes/string_42.h"
char *ft_utoa_base(uintmax_t n, char *base, int precision)
{
unsigned short intbase;
unsigned short col;
char *buf;
if (!valid_base(base))
return (NULL);
col = ft_uintmaxlen_base(n, ft_strlen(base));
precision = (precision - col > 0) ? precision - col : 0;
col += precision;
if (!(buf = malloc(col + 1)))
return (NULL);
buf = ft_memset(buf, '0', col);
buf[col] = '\0';
intbase = ft_strlen(base);
while (col - precision > 0)
{
buf[--col] = base[n % intbase];
n /= intbase;
}
return (buf);
}