This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
executable file
·60 lines (55 loc) · 1.63 KB
/
ft_itoa.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
57
58
59
60
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa.c :+: :+: */
/* +:+ */
/* By: fbes <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/10/27 16:32:18 by fbes #+# #+# */
/* Updated: 2022/04/09 00:39:55 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static char *ft_itoad(unsigned int n, int neg, int digits)
{
char *res;
int i;
res = ft_stralloc(digits);
if (res)
{
i = digits - 1;
while (n > 0)
{
res[i] = (n % 10) + '0';
n /= 10;
i--;
}
if (neg)
res[i] = '-';
}
return (res);
}
/**
* Convert a number of base10 into a string
* @param[in] n The number to convert
* @return The converted number in string format, NULL on error
*/
char *ft_itoa(int n)
{
int digits;
int neg;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n == 0)
return (ft_strdup("0"));
digits = ft_numlen((unsigned int)ft_abs(n), 10);
neg = 0;
if (n < 0)
{
digits++;
n *= -1;
neg = 1;
}
return (ft_itoad((unsigned int)n, neg, digits));
}