-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_hexa_ltoa.c
56 lines (52 loc) · 1.48 KB
/
ft_hexa_ltoa.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_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rabougue <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/07/09 03:58:17 by rabougue #+# #+# */
/* Updated: 2016/08/06 22:15:07 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_ltoa(unsigned long long n, int up)
{
char *str;
int size;
unsigned long long 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);
}