-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_bases.c
84 lines (76 loc) · 2.27 KB
/
print_bases.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
#include "main.h"
/**
* print_hex - prints a number in hexadecimal base,
* in lowercase
* @l: va_list arguments from _printf
* @f: pointer to the struct flags that determines
* if a flag is passed to _printf
* Description: the function calls convert() which in turns converts the input
* number into the correct base and returns it as a string
* Return: the number of char printed
*/
int print_hex(va_list l, flags_t *f)
{
unsigned int num = va_arg(l, unsigned int);
char *str = convert(num, 16, 1);
int count = 0;
if (f->hash == 1 && str[0] != '0')
count += _puts("0x");
count += _puts(str);
return (count);
}
/**
* print_hex_big - prints a number in hexadecimal base,
* in uppercase
* @l: va_list arguments from _printf
* @f: pointer to the struct that determines
* if a flag is passed to _printf
* Description: the function calls convert() which in turns converts the input
* number into the correct base and returns it as a string
* Return: the number of char printed
*/
int print_hex_big(va_list l, flags_t *f)
{
unsigned int num = va_arg(l, unsigned int);
char *str = convert(num, 16, 0);
int count = 0;
if (f->hash == 1 && str[0] != '0')
count += _puts("0X");
count += _puts(str);
return (count);
}
/**
* print_binary - prints a number in base 2
* @l: va_list arguments from _printf
* @f: pointer to the struct that determines
* if a flag is passed to _printf
* Description: the function calls convert() which in turns converts the input
* number into the correct base and returns it as a string
* Return: the number of char printed
*/
int print_binary(va_list l, flags_t *f)
{
unsigned int num = va_arg(l, unsigned int);
char *str = convert(num, 2, 0);
(void)f;
return (_puts(str));
}
/**
* print_octal - prints a number in base 8
* @l: va_list arguments from _printf
* @f: pointer to the struct that determines
* if a flag is passed to _printf
* Description: the function calls convert() which in turns converts the input
* number into the correct base and returns it as a string
* Return: the number of char printed
*/
int print_octal(va_list l, flags_t *f)
{
unsigned int num = va_arg(l, unsigned int);
char *str = convert(num, 8, 0);
int count = 0;
if (f->hash == 1 && str[0] != '0')
count += _putchar('0');
count += _puts(str);
return (count);
}