-
Notifications
You must be signed in to change notification settings - Fork 0
/
uitoa.c
72 lines (63 loc) · 1.61 KB
/
uitoa.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
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* :::::::: */
/* uitoa.c :+: :+: */
/* +:+ */
/* By: kgajadie <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/22 12:02:05 by kgajadie #+# #+# */
/* Updated: 2021/03/06 12:30:21 by kawish ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static unsigned int ft_int2pwr(int x, int n)
{
int i;
unsigned int result;
i = 0;
result = 1;
while (i < n)
{
result *= x;
i++;
}
return (result);
}
static size_t ft_intlen(unsigned int n)
{
size_t len;
len = 1;
if (n < 0)
{
n = n * -1;
len++;
}
while (n > 9)
{
len++;
n = n / 10;
}
return (len);
}
/* Variation on libft's itoa to work with unsigned int.
unsigned int n -- number to convert
*/
char *uitoa(unsigned int n)
{
unsigned int i;
size_t len;
char *str;
i = 0;
len = ft_intlen(n);
str = ft_calloc(len + 1, sizeof(*str));
if (!str)
return (0);
len = len - i;
while (len > 0)
{
str[i] = ((n / ft_int2pwr(10, len - 1)) % 10) + '0';
i++;
len--;
}
return (str);
}