-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbrX.c
60 lines (52 loc) · 942 Bytes
/
ft_putnbrX.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
#include <unistd.h>
#include <stdlib.h>
int nb_size(unsigned int n, int base)
{
int size;
size = 1;
while (n >= (unsigned int)base)
{
n /= base;
size++;
}
size++;
return (size);
}
void nb_print(unsigned int nbr, int base, char *result, char *str)
{
int aux;
aux = 0;
while (nbr >= (unsigned int)base)
{
*(result + aux) = *(str + (nbr % base));
nbr /= base;
aux++;
}
*(result + aux) = *(str + nbr);
while ((result + aux) >= result)
{
write (1, &*(result + aux), 1);
aux--;
}
}
void nb_convert(char *str, unsigned int n, int size, int base)
{
char *result;
result = malloc(size * sizeof(char));
if (result)
{
nb_print(n, base, result, str);
free (result);
}
}
void ft_putnbrX(unsigned int n, char x)
{
int base;
int size;
base = 16;
size = nb_size(n, base);
if (x == 'x')
nb_convert("0123456789abcdef", n, size, base);
else if (x == 'X')
nb_convert("0123456789ABCDEF", n, size, base);
}