-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_put_hexa_up.c
74 lines (67 loc) · 1.64 KB
/
ft_put_hexa_up.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
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_hexa_up.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marmoral <marmoral@student.42wolfsburg. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/26 17:05:53 by marmoral #+# #+# */
/* Updated: 2022/06/07 12:07:35 by marmoral ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t digits(unsigned long n_cpy)
{
size_t i;
i = 0;
if (n_cpy == 0)
return (1);
while (n_cpy > 0)
{
i++;
n_cpy /= 16;
}
return (i);
}
static void transform(char *nbr, size_t i, unsigned long n_cpy)
{
int r;
char *hex;
r = 0;
hex = "0123456789ABCDEF";
if (n_cpy == 0)
nbr[i] = hex[r];
while (n_cpy > 0)
{
r = n_cpy % 16;
nbr[i] = hex[r];
n_cpy /= 16;
i--;
}
}
int ft_put_hexa_up(int n)
{
unsigned long n_cpy;
char *nbr;
long nc;
size_t i;
size_t di;
i = 0;
di = 0;
nc = n;
if (n < 0)
{
nc *= -1;
n_cpy = (4294967295 + 1) - nc;
}
else
n_cpy = n;
di = digits(n_cpy);
i = di;
nbr = malloc(i + 1);
nbr[i--] = 0;
transform(nbr, i, n_cpy);
ft_putstr_fd(nbr, 1);
free (nbr);
return (di);
}