-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_stoa_base.c
56 lines (51 loc) · 1.55 KB
/
ft_stoa_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mfrisby <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/07/11 13:28:26 by mfrisby #+# #+# */
/* Updated: 2017/07/11 13:28:27 by mfrisby ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static int ft_nblen_base(long long n, int base)
{
int len;
long double pow;
pow = 1;
len = 0;
if (n < 0)
len++;
n < 0 ? n = -n : 0;
while (n >= (pow *= base))
len++;
return (len + 1);
}
char *ft_stoa_base(long long nb, int base)
{
char *s;
int len;
s = NULL;
if (nb == -9223372036854775807 - 1)
return (ft_strdup("-9223372036854775808"));
len = ft_nblen_base(nb, base);
if (!(s = ft_strnew(len)))
return (NULL);
if (nb < 0)
s[0] = '-';
nb < 0 ? nb = -nb : 0;
if (nb == 0)
{
s[0] = '0';
s[1] = 0;
return (s);
}
while (nb)
{
s[--len] = nb % base > 9 ? nb % base - 10 + 'a' : nb % base + '0';
nb /= base;
}
return (s);
}