-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa_base.c
executable file
·42 lines (39 loc) · 1.53 KB
/
ft_itoa_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
39
40
41
42
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/21 16:08:52 by akharrou #+# #+# */
/* Updated: 2019/04/18 20:34:59 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Includes/stdlib_42.h"
#include "../Includes/string_42.h"
char *ft_itoa_base(intmax_t n, char *base, int precision)
{
unsigned short intbase;
unsigned short col;
intmax_t val;
char *buf;
if (!valid_base(base))
return (NULL);
col = ft_intmaxlen_base(n, ft_strlen(base));
precision = ((precision - col > 0) ? precision - col : 0);
col += precision + (n < 0);
if (!(buf = malloc(col + 1)))
return (NULL);
buf = ft_memset(buf, '0', col);
buf[col] = '\0';
intbase = ft_strlen(base);
val = (n < 0) ? -n : n;
while (col - precision > 0)
{
buf[--col] = base[val % intbase];
val /= intbase;
}
if (n < 0)
(*buf) = '-';
return (buf);
}