-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_tohexadecimal.c
60 lines (54 loc) · 1.79 KB
/
ft_tohexadecimal.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tohexadecimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jvico-ga <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 20:12:10 by jvico-ga #+# #+# */
/* Updated: 2021/11/05 20:58:43 by jvico-ga ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void invert_num(unsigned long long num, int mayus,
char *invertido, size_t j);
size_t ft_tohexadecimal(unsigned long long num, int mayus)
{
unsigned long long conversor;
char aux;
char *invertido;
size_t j;
unsigned long long aux_num;
if (num == 0)
return (ft_putchar_fd('0', 1));
aux_num = num;
j = 0;
while (num != 0 && j++ != -1)
num /= 16;
invertido = malloc(sizeof(char) * j);
if (invertido == NULL)
return (0);
invert_num(aux_num, mayus, invertido, j);
return (ft_putstr_fd(invertido, 1));
}
static void invert_num(unsigned long long num, int mayus,
char *invertido, size_t j)
{
unsigned long long conversor;
char aux;
int isMayus;
if (mayus == 0)
isMayus = 87;
else
isMayus = 55;
while (num != 0)
{
conversor = num % 16;
if (conversor < 10)
aux = 48 + conversor;
else
aux = isMayus + conversor;
invertido[--j] = aux;
num /= 16;
}
}