-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_utoa_base.c
More file actions
75 lines (65 loc) · 1.92 KB
/
Copy pathft_utoa_base.c
File metadata and controls
75 lines (65 loc) · 1.92 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
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joleksia <joleksia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/13 08:12:29 by joleksia #+# #+# */
/* Updated: 2024/12/22 16:04:36 by joleksia ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_utils.h"
#include "./libft/libft.h"
static unsigned int fts_numlen(unsigned int n, int radix);
static char *fts_strprc(char *s, unsigned int n, const char *b, int r);
static char *fts_strrev(char *s, size_t len);
char *ft_utoa_base(unsigned int u, const char *base, int radix)
{
char *result;
size_t nlen;
nlen = fts_numlen(u, radix);
result = (char *) ft_calloc(nlen + 1, sizeof(char));
if (!result)
return (NULL);
result = fts_strprc(result, u, base, radix);
return (result);
}
static unsigned int fts_numlen(unsigned int n, int radix)
{
int result;
result = 1;
while (n)
{
n /= radix;
if (n)
result++;
}
return (result);
}
static char *fts_strprc(char *s, unsigned int n, const char *b, int r)
{
char *scpy;
scpy = s;
if (n == 0)
*s++ = '0';
while (n)
{
*s++ = b[n % r];
n /= r;
}
return (fts_strrev(scpy, ft_strlen(scpy)));
}
static char *fts_strrev(char *s, size_t len)
{
size_t i;
char temp;
i = -1;
while (++i < len / 2)
{
temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
return (s);
}