-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_puthex_fd.c
61 lines (56 loc) · 1.63 KB
/
ft_puthex_fd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jeshin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 17:14:58 by jeshin #+# #+# */
/* Updated: 2023/12/09 18:34:08 by jeshin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_puthex_fd(long int n, int fd, int upper)
{
long int l;
char *set;
l = (long int)n;
if (upper)
set = "0123456789ABCDEF";
else
set = "0123456789abcdef";
if (l < 0)
{
l *= -1;
write(fd, "-", 1);
}
if (l < 16)
write(fd, &set[l], 1);
else if (l >= 16)
{
ft_puthex_fd(l / 16, 1, upper);
write(fd, &set[l % 16], 1);
}
}
void ft_puthex_cnt(unsigned long int n, int upper, int *num)
{
unsigned long int l;
char *set;
l = (unsigned long int)n;
if (upper)
set = "0123456789ABCDEF";
else
set = "0123456789abcdef";
if (l < 0)
{
l *= -1;
*num += write(1, "-", 1);
}
if (l < 16)
*num += write(1, &set[l], 1);
else if (l >= 16)
{
ft_puthex_cnt(l / 16, upper, num);
*num += write(1, &set[l % 16], 1);
}
}