-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_base.c
37 lines (34 loc) · 1.24 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bwebb <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/26 11:59:07 by bwebb #+# #+# */
/* Updated: 2019/07/18 17:14:59 by bwebb ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa_base(int in, int b)
{
char str[100];
char *s;
int k;
int j;
k = 0;
j = ((in < 0) && (b == 10)) ? 1 : 0;
while ((in != 0) || (k == 0))
{
str[k++] = BASE_VALS[ft_abs(in % b)];
in /= b;
}
if (!(s = ft_strnew(k + j)))
return (NULL);
if (j == 1)
s[0] = '-';
while (k > 0)
s[j++] = str[--k];
s[j] = '\0';
return (s);
}