-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_uint_to_hex_uplow.c
73 lines (67 loc) · 1.77 KB
/
ft_uint_to_hex_uplow.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_uint_to_hex_uplow.c :+: :+: */
/* +:+ */
/* By: splattje <splattje@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/07 15:01:42 by splattje #+# #+# */
/* Updated: 2023/11/14 10:42:11 by splattje ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int get_length(unsigned int n)
{
int length;
int num;
if (n == 0)
length = 1;
else
length = 0;
num = n;
while (n > 0)
{
n >>= 4;
length++;
}
return (length);
}
static char get_char(int nibble, int uplow)
{
if (nibble < 10)
return ('0' + nibble);
else
{
if (uplow == 1)
return ('a' + nibble - 10);
else
return ('A' + nibble - 10);
}
}
char *uint_to_hex_uplow(unsigned int num, int uplow)
{
unsigned int length;
unsigned int n;
char *hex_str;
int index;
int nibble;
if (uplow > 1)
return (NULL);
length = get_length(num);
hex_str = (char *)malloc(length + 1);
if (hex_str == NULL)
return (NULL);
hex_str = ft_memset(hex_str, 0, length + 1);
index = length - 1;
n = num;
if (n == 0)
hex_str[0] = '0';
while (n > 0)
{
nibble = n & 0xF;
hex_str[index] = get_char(nibble, uplow);
n >>= 4;
index--;
}
return (hex_str);
}