-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa_unsigned.c
More file actions
68 lines (63 loc) · 1.83 KB
/
Copy pathft_itoa_unsigned.c
File metadata and controls
68 lines (63 loc) · 1.83 KB
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_itoa_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yyefimov <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 14:54:46 by yyefimov #+# #+# */
/* Updated: 2017/02/18 14:57:30 by yyefimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
static void nbr_to_upstr(unsigned long long v, unsigned long long b, char **str)
{
if (v >= b)
{
nbr_to_upstr(v / b, b, str);
nbr_to_upstr(v % b, b, str);
}
else
{
if (v >= 10)
**str = v + 'A' - 10;
else
**str = v + '0';
(*str)++;
}
}
static void nbr_to_lowstr(unsigned long long v, unsigned long long b, char **s)
{
if (v >= b)
{
nbr_to_lowstr(v / b, b, s);
nbr_to_lowstr(v % b, b, s);
}
else
{
if (v >= 10)
**s = v + 'a' - 10;
else
**s = v + '0';
(*s)++;
}
}
char *ft_itoa_unsigned(unsigned long long value, int base, char mod)
{
char *str;
char *p;
int i;
i = 0;
str = (char*)malloc(sizeof(char) * 33);
while (i < 33)
{
str[i] = 0;
i++;
}
p = str;
if (base > 1 && base < 17 && mod == 'X')
nbr_to_upstr((unsigned long long)value, (unsigned int)base, &p);
else if ((base > 1 && base < 17) || mod == 'x')
nbr_to_lowstr((unsigned long long)value, (unsigned int)base, &p);
return (str);
}