-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_hexa_min.c
71 lines (64 loc) · 1.77 KB
/
ft_hexa_min.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexa_min.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gemartin <gemartin@student.42barc...> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/06 18:27:59 by gemartin #+# #+# */
/* Updated: 2022/06/08 15:39:36 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int puthexa_long(char *bstr, unsigned long long n, int c)
{
unsigned long long b;
b = ft_strlen(bstr);
if (n >= b)
{
c = puthexa_long(bstr, n / b, c);
if (c == -1)
return (-1);
if (write (1, &bstr[n % b], 1) == -1)
return (-1);
c++;
}
else if (n < b)
{
if (write (1, &bstr[n], 1) == -1)
return (-1);
c++;
}
return (c);
}
static int puthexa_uns(char *bstr, unsigned int n, int c)
{
unsigned int b;
b = ft_strlen(bstr);
if (n >= b)
{
c = puthexa_uns(bstr, n / b, c);
if (c == -1)
return (-1);
if (write (1, &bstr[n % b], 1) == -1)
return (-1);
c++;
}
else if (n < b)
{
if (write (1, &bstr[n], 1) == -1)
return (-1);
c++;
}
return (c);
}
int ft_hexa_min(int n)
{
int let;
let = 0;
if (n >= 0)
let = puthexa_long("0123456789abcdef", n, let);
else if (n < 0)
let = puthexa_uns("0123456789abcdef", n, let);
return (let);
}