-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_putnbr_base.c
58 lines (51 loc) · 1.3 KB
/
my_putnbr_base.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
/*
** EPITECH PROJECT, 2020
** B-PSU-100-NCY-1-1-myprintf-sebastien.raoult
** File description:
** my_put_base
*/
#include "my.h"
static int calc_r_value(unsigned long nb, unsigned long ret, size_t base_len)
{
int r_value = 0;
while ((nb / ret) >= base_len) {
ret *= base_len;
}
while (ret > 0) {
r_value++;
ret /= base_len;
}
return (r_value);
}
int my_putnbr_base(unsigned long nb, char *base)
{
size_t base_len = my_strlen(base);
unsigned long ret = 1;
int r_value = calc_r_value(nb, ret, base_len);
for (ret = 1; (nb / ret) >= base_len; ret *= base_len);
while (ret > 0) {
my_putchar(base[(nb / ret) % base_len]);
ret /= base_len;
}
return (r_value);
}
int my_put_nonprintable(unsigned long nb, char *base)
{
size_t base_len = my_strlen(base);
unsigned long ret = 1;
int i = 0;
int j = 0;
int r_value = calc_r_value(nb, ret, base_len);
char *res = malloc(r_value + 1);
for (ret = 1; (nb / ret) >= base_len; ret *= base_len);
while (ret > 0) {
res[i++] = base[(nb / ret) % base_len];
ret /= base_len;
}
res[i] = 0;
for (j = my_strlen(res); j < 3; j++)
my_putchar('0');
my_putstr(res);
free(res);
return ((r_value >= 3) ? r_value : r_value + (j - 1));
}