-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp_hex_utils.c
56 lines (51 loc) · 1.49 KB
/
ftp_hex_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ftp_hex_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ffrau <ffrau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/23 01:11:25 by ffrau #+# #+# */
/* Updated: 2022/05/23 01:11:26 by ffrau ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_hex_len(unsigned int num)
{
int len;
len = 0;
while (num != 0)
{
len++;
num = num / 16;
}
return (len);
}
void ft_put_hex(unsigned int num, const char format)
{
if (num >= 16)
{
ft_put_hex(num / 16, format);
ft_put_hex(num % 16, format);
}
else
{
if (num <= 9)
ft_putchar((num + '0'));
else
{
if (format == 'x')
ft_putchar((num - 10 + 'a'));
if (format == 'X')
ft_putchar((num - 10 + 'A'));
}
}
}
int ft_print_hex(unsigned int num, const char format)
{
if (num == 0)
return (write(1, "0", 1));
else
ft_put_hex(num, format);
return (ft_hex_len(num));
}