-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base_bonus.c
39 lines (37 loc) · 1.31 KB
/
ft_itoa_base_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: agoksu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 17:53:14 by agoksu #+# #+# */
/* Updated: 2022/10/05 17:53:17 by agoksu ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_itoa_base(int n, int base)
{
int len;
char *ret;
const char *digits = "0123456789abcdef";
len = ft_numlen(n, base);
ret = malloc(sizeof(char) * (len + 1));
if (!ret)
return (0);
ret[len] = 0;
if (n == 0)
ret[0] = '0';
if (n < 0)
ret[0] = '-';
while (n)
{
if (n > 0)
ret[--len] = digits[n % base];
else
ret[--len] = digits[-n % base * -1];
n /= base;
}
return (ret);
}