-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_help1.c
86 lines (77 loc) · 1.88 KB
/
functions_help1.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
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functions_help1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alvachon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 19:51:50 by alvachon #+# #+# */
/* Updated: 2022/07/22 11:57:21 by alvachon ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static unsigned int ft_ptoa_len(unsigned long n)
{
unsigned int a;
if (n == 0)
return (0);
a = 0;
while (n != 0)
{
n = n / 16;
a++;
}
return (a);
}
static unsigned int ft_utoa_len(unsigned int n)
{
unsigned int a;
if (n == 0)
return (1);
a = 0;
while (n != 0)
{
a += 1;
n = n / 10;
}
return (a);
}
char *ft_deal_ptr(unsigned long n)
{
char *temp;
char *digits;
int len;
if (n == 0)
return (ft_strdup("0"));
len = ft_ptoa_len(n);
digits = "0123456789abcdef";
temp = (char *)malloc(sizeof(char) * len + 1);
if (!temp)
return (NULL);
temp[len] = '\0';
while (n != 0)
{
temp[len - 1] = digits[n % 16];
n = n / 16;
len--;
}
return (temp);
}
char *ft_deal_usign(unsigned int n)
{
char *temp;
unsigned int len;
if (n == 0)
return (ft_strdup("0"));
len = ft_utoa_len(n);
temp = (char *)malloc(sizeof(char) * len + 1);
if (!temp)
return (NULL);
temp[len] = '\0';
while (n != 0)
{
temp[--len] = (n % 10) + '0';
n = n / 10;
}
return (temp);
}