-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_hexlow.c
69 lines (62 loc) · 1.58 KB
/
print_hexlow.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_hexlow.c :+: :+: */
/* +:+ */
/* By: dreijans <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/21 18:54:57 by dreijans #+# #+# */
/* Updated: 2022/12/05 13:58:56 by dreijans ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int how_much(unsigned int a)
{
int i;
i = 0;
if (a == 0)
i++;
while (a != 0)
{
a = a / 16;
i++;
}
return (i);
}
static char *hex_toa(unsigned int n)
{
char *str;
int index;
int mod;
index = how_much(n);
str = ft_calloc(index + 1, sizeof (char));
if (str == NULL)
return (NULL);
index--;
if (n == 0)
str[0] = '0';
while (n != 0)
{
mod = n % 16;
if (mod < 10)
str[index] = mod + '0';
else
str[index] = (mod - 10) + 'a';
n = n / 16;
index--;
}
return (str);
}
int print_hexlow(unsigned int n)
{
int count;
char *nbr;
count = -1;
nbr = hex_toa(n);
if (nbr)
{
count = write(1, nbr, ft_strlen(nbr));
free (nbr);
}
return (count);
}