-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoa_malloc.c
More file actions
56 lines (50 loc) · 1.86 KB
/
toa_malloc.c
File metadata and controls
56 lines (50 loc) · 1.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* toa_malloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: monoue <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/06 16:00:02 by monoue #+# #+# */
/* Updated: 2020/08/20 15:14:45 by monoue ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char *ft_ctoa_malloc(unsigned int c)
{
char *mc;
mc = ft_strdup("0");
if (!mc)
return (NULL);
mc[0] = c;
return (mc);
}
char *ft_utoa_malloc(unsigned int num)
{
if (num >= 10)
return (ft_strjoin_free_both(ft_utoa_malloc(num / 10),
ft_utoa_malloc(num % 10)));
else
return (ft_ctoa_malloc((char)(num + '0')));
}
char *ft_itoa_malloc(long num)
{
if (num < 0)
return (ft_strjoin_free_both(ft_ctoa_malloc('-'),
ft_itoa_malloc(-num)));
else if (num >= 10)
return (ft_strjoin_free_both(ft_itoa_malloc(num / 10),
ft_itoa_malloc(num % 10)));
else
return (ft_ctoa_malloc((char)(num + '0')));
}
char *ft_xtoa_malloc(unsigned long long num, t_format_info *info)
{
if (num >= 16)
return (ft_strjoin_free_both(ft_xtoa_malloc(num / 16, info),
ft_xtoa_malloc(num % 16, info)));
else if (info->conv_c == 'X')
return (ft_ctoa_malloc((char)("0123456789ABCDEF"[num])));
else
return (ft_ctoa_malloc("0123456789abcdef"[num]));
}