-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_nb_conversions.c
126 lines (112 loc) · 2.69 KB
/
ft_nb_conversions.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nb_conversions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcosta-d <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/27 22:29:35 by mcosta-d #+# #+# */
/* Updated: 2023/06/27 22:31:19 by mcosta-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*3. Number conversion - %d - Prints a decimal (base 10) number
%i - Prints an integer in base 10
Without modifiers, they are basically the same*/
void ft_putnbr(long long int nbr, int *i)
{
if (nbr < 0)
{
nbr = -nbr;
*i += write(1, "-", 1);
}
if (nbr >= 10)
{
ft_putnbr(nbr / 10, i);
ft_putnbr(nbr % 10, i);
}
else
{
nbr += 48;
*i += write(1, &nbr, 1);
}
}
/*4. Unsigned number conversion - %u - Prints an unsigned
decimal (base 10) number.*/
void ft_putnbr_unsigned(unsigned int nbr, int *i)
{
if (nbr >= 10)
{
ft_putnbr_unsigned(nbr / 10, i);
ft_putnbr_unsigned(nbr % 10, i);
}
else
{
nbr += 48;
*i += write(1, &nbr, 1);
}
}
/*5. Hexadecimal lowercase - %x - Prints a number in
hexadecimal (base 16) lowercase format.*/
void ft_puthexa_low(unsigned int nbr, int *i)
{
char str[20];
int index;
index = 0;
if (nbr == 0)
ft_putchar('0', i);
else
{
while (nbr != 0)
{
str[index] = "0123456789abcdef"[nbr % 16];
nbr /= 16;
index++;
}
while (index--)
ft_putchar(str[index], i);
}
}
/*5. Hexadecimal uppercase - %X - Prints a number in
hexadecimal (base 16) uppercase format.*/
void ft_puthexa_up(unsigned int nbr, int *i)
{
char str[20];
int index;
index = 0;
if (nbr == 0)
ft_putchar('0', i);
while (nbr != 0)
{
str[index] = "0123456789ABCDEF"[nbr % 16];
nbr /= 16;
index++;
}
while (index--)
ft_putchar(str[index], i);
}
/*6. Adress (pointers) - %p - The void * pointer
argument has to be printed in hexadecimal format.*/
void ft_putadress(unsigned long nbr, int *i)
{
char str[25];
int index;
index = 0;
if (nbr == 0)
{
ft_putstr("(nil)", i);
return ;
}
else
{
ft_putstr("0x", i);
}
while (nbr != 0)
{
str[index] = "0123456789abcdef"[nbr % 16];
nbr /= 16;
index++;
}
while (index--)
ft_putchar(str[index], i);
}