-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_convert_hex.c
68 lines (61 loc) · 1.83 KB
/
ft_convert_hex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aestrell <aestrell@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/17 14:12:07 by aestrell #+# #+# */
/* Updated: 2024/03/17 14:12:07 by aestrell ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_result_hex(char *result_hex)
{
int i;
int j;
char temp;
i = 0;
j = (int)ft_strlen(result_hex) - 1;
while (i < j)
{
temp = result_hex[i];
result_hex[i] = result_hex[j];
result_hex[j] = temp;
i++;
j--;
}
}
static char *ft_verify_format(char flag)
{
char *hex_format;
hex_format = "0123456789abcdef";
if (flag == 'X')
hex_format = "0123456789ABCDEF";
return (hex_format);
}
char *ft_convert_to_hex(unsigned long long number, char flag)
{
char *hex;
char *result_hex;
int i;
hex = ft_verify_format(flag);
result_hex = (char *)malloc(sizeof(char) * 17);
if (result_hex == NULL)
return (NULL);
i = 0;
if (number == 0)
result_hex[i] = '0';
else
{
while (number != 0 && i < 16)
{
result_hex[i] = hex[number % 16];
number = number / 16;
i++;
}
}
result_hex[i] = '\0';
ft_result_hex(result_hex);
return (result_hex);
}