-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_hexa_itoa.c
56 lines (52 loc) · 1.47 KB
/
ft_hexa_itoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexa_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rabougue <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/07/05 05:59:12 by rabougue #+# #+# */
/* Updated: 2016/11/18 11:32:02 by rabougue ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/libft.h"
static int switch_hexa(int x, int up)
{
if (0 <= x && x <= 9)
return (48 + x);
if (x >= 10 && x <= 15)
{
x = x - 10;
if (up == 0)
return ('a' + x);
else if (up == 1)
return ('A' + x);
}
return (0);
}
char *ft_hexa_itoa(unsigned int n, int up)
{
char *str;
int size;
unsigned int x;
x = n;
size = 0;
while (x >= 16)
{
x /= 16;
size++;
}
str = (char *)malloc(sizeof(char) * (size + 1));
if (str)
{
str[size + 1] = '\0';
while (size >= 0)
{
x = n % 16;
str[size] = switch_hexa(x, up);
n /= 16;
size--;
}
}
return (str);
}