-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf_utils.c
93 lines (84 loc) · 1.97 KB
/
printf_utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <melkholy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/25 13:54:08 by melkholy #+# #+# */
/* Updated: 2022/08/26 06:15:36 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_ncount_unsigned(unsigned int n)
{
unsigned int temp;
int count;
count = 0;
if (n == 0)
return (1);
temp = n;
while (temp)
{
temp /= 10;
count ++;
}
return (count);
}
char *ft_itoa_unsigned(unsigned int n)
{
int count;
char *num;
count = ft_ncount_unsigned(n);
num = (char *)malloc(count + 1 * sizeof(char));
if (!num)
return (NULL);
num[count] = '\0';
while (--count >= 0 || n)
{
num[count] = (n % 10) + 48;
n /= 10;
}
return (num);
}
int ft_ncount_hex(size_t n)
{
int count;
size_t temp;
count = 0;
if (n == 0)
return (1);
temp = n;
while (temp)
{
temp /= 16;
count ++;
}
return (count);
}
char *ft_to_hexstr(char speci, size_t n)
{
int count;
int ptr;
char *num;
char *base;
ptr = 0;
if (speci == 'p')
ptr = 2;
if (speci == 'X')
base = "0123456789ABCDEF";
else
base = "0123456789abcdef";
count = ft_ncount_hex(n);
count += ptr;
num = (char *)malloc(count + 1 * sizeof(char));
if (!num)
return (NULL);
num[count] = '\0';
while (--count >= 0 || n)
{
num[count] = base[n % 16];
n /= 16;
}
return (num);
}