-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa_base.c
40 lines (34 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/15 11:04:07 by dskrypny #+# #+# */
/* Updated: 2018/05/27 15:09:44 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#define ABS(x) ((x) < 0 ? -(x) : (x))
void make(long value, long base, char *str, int *i)
{
char *tmp;
tmp = "0123456789abcdef";
if (value <= -base || value >= base)
make(value / base, base, str, i);
str[(*i)++] = tmp[ABS(value % base)];
}
char *ft_itoa_base(long value, long base)
{
int i;
char *str;
i = 0;
if (base < 2 || base > 16 || !(str = (char *)malloc(32)))
return (0);
if (base == 10 && value < 0)
str[i++] = '-';
make(value, base, str, &i);
str[i] = '\0';
return (str);
}