-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_puthex.c
70 lines (63 loc) · 1.59 KB
/
ft_puthex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/10 12:13:33 by phelebra #+# #+# */
/* Updated: 2023/02/10 12:53:09 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_sizehex(unsigned int ui)
{
int i;
i = 0;
while (ui)
{
i++;
ui /= 16;
}
return (i);
}
static char *ft_itohex(unsigned int ui, char xchar)
{
char *ptr;
int size;
int i;
size = ft_sizehex(ui);
ptr = malloc(sizeof(char) * (size + 1));
if (!ptr)
return (NULL);
ptr[size] = '\0';
while (ui)
{
i = ui % 16;
if (i < 10)
ptr[size - 1] = i + '0';
else
ptr[size - 1] = i + xchar;
ui /= 16;
size--;
}
return (ptr);
}
int ft_puthex(unsigned int ui, char xchar)
{
char *s;
int size;
if (!ui)
{
write (1, "0", 1);
return (1);
}
if (xchar == 'X')
xchar = 55;
else
xchar = 87;
s = ft_itohex(ui, xchar);
size = ft_putstr(s);
free(s);
return (size);
}