-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_hexadecimals.c
59 lines (53 loc) · 1.71 KB
/
ft_hexadecimals.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexadecimals.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmacias- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/23 17:49:48 by gmacias- #+# #+# */
/* Updated: 2022/02/23 19:04:49 by gmacias- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_length_hexadecimal(unsigned int num);
static void ft_search_hexadecimal(unsigned int num, const char word);
int ft_print_hexadecimal(unsigned int num, const char word)
{
if (num == 0)
return (ft_print_character('0'));
else
ft_search_hexadecimal(num, word);
return (ft_length_hexadecimal(num));
}
static void ft_search_hexadecimal(unsigned int num, const char word)
{
if (num >= 16)
{
ft_search_hexadecimal(num / 16, word);
ft_search_hexadecimal(num % 16, word);
}
else
{
if (num < 10)
ft_print_character(num + '0');
else
{
if (word == 'x')
ft_print_character(num - 10 + 'a');
if (word == 'X')
ft_print_character(num - 10 + 'A');
}
}
}
static int ft_length_hexadecimal(unsigned int num)
{
int len;
len = 0;
while (num != 0)
{
len++;
num = num / 16;
}
return (len);
}