-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
84 lines (77 loc) · 2.03 KB
/
ft_putnbr_fd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/14 15:18:30 by jbadaire #+# #+# */
/* Updated: 2023/10/14 15:18:30 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putnbr_fd(int n, int fd)
{
unsigned int nb;
char ch;
int value;
value = 0;
nb = (unsigned int)n;
if (n < 0)
{
nb = nb * -1;
value += write (fd, "-", 1);
}
if (nb <= 9)
{
ch = nb + '0';
value += write(fd, &ch, 1);
}
if (nb > 9)
{
value += ft_putnbr_fd(nb / 10, fd);
value += ft_putnbr_fd(nb % 10, fd);
}
return (value);
}
int ft_put_unsigned_nbr_fd(unsigned int n, int fd)
{
unsigned int nb;
char ch;
int value;
nb = n;
ch = 0;
value = 0;
if (nb <= 9)
{
ch = nb + '0';
value = write(fd, &ch, 1);
}
if (nb > 9)
{
value = 0;
value += ft_put_unsigned_nbr_fd(nb / 10, fd);
value += ft_put_unsigned_nbr_fd(nb % 10, fd);
}
return (value);
}
int ft_putnbr_base(long long int number, int base_number, \
char base_array[], int value)
{
char character;
value = 0;
if (number >= (long long int)base_number)
{
value += ft_putnbr_base(number / base_number, \
base_number, base_array, value);
value += ft_putnbr_base(number % base_number, \
base_number, base_array, value);
}
else
{
character = base_array[number];
value += ft_putchar_fd(character, 1);
return (value);
}
return (value);
}