-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_puthex_pf.c
79 lines (70 loc) · 1.74 KB
/
ft_puthex_pf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rahidalg <rahidalg@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 19:13:27 by rahidalg #+# #+# */
/* Updated: 2024/05/02 19:13:39 by rahidalg ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static size_t ft_uintdigits(unsigned int n)
{
size_t i;
i = 0;
if (n == 0)
return (1);
while (n)
{
n /= 16;
i++;
}
return (i);
}
static void ft_print_revstr(char *s, size_t type, int fd)
{
size_t i;
i = ft_strlen(s);
while (i > 0)
{
i--;
if (type)
s[i] = ft_tolower(s[i]);
ft_putchar_fd(s[i], fd);
}
}
static void ft_hextostr(unsigned int n, char *s)
{
unsigned int tmp;
size_t i;
tmp = 0;
i = 0;
while (n != 0)
{
tmp = n % 16;
if (tmp < 10)
tmp = tmp + 48;
else
tmp = tmp + 55;
s[i] = tmp;
n /= 16;
i++;
}
}
int ft_puthex_pf(unsigned int n, size_t type, int fd)
{
size_t size;
char *out;
size = ft_uintdigits(n);
out = (char *)malloc(sizeof(char) * (size + 1));
if (n == 0)
out[0] = '0';
else
ft_hextostr(n, out);
out[size] = '\0';
ft_print_revstr(out, type, fd);
free(out);
return (size);
}