forked from Pierre-Dureau/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_base.c
executable file
·98 lines (94 loc) · 2.05 KB
/
print_base.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
#include "main.h"
/**
* print_binary - convert into binary and print
* @arg: parameter to convert
* @flag: flag parameter
* Return: number of printed char
*/
int print_binary(va_list arg, flag_t flag)
{
int nb_char = 0;
unsigned int num = va_arg(arg, unsigned int);
static char buffer[33];
char *ptr;
ptr = &buffer[32];
*ptr = '\0';
do {
*--ptr = (num % 2) + '0';
num /= 2;
} while (num != 0);
nb_char = _puts(ptr);
(void)flag;
return (nb_char);
}
/**
* print_octal - convert into octal and print
* @arg: parameter to convert
* @flag: flag parameter
* Return: number of printed char
*/
int print_octal(va_list arg, flag_t flag)
{
int nb_char = 0;
unsigned int num = va_arg(arg, unsigned int), base = num;
static char buffer[13];
char *ptr;
ptr = &buffer[12];
*ptr = '\0';
do {
*--ptr = (num % 8) + '0';
num /= 8;
} while (num != 0);
if (flag.diese == 1 && base != 0)
nb_char += _putchar('0');
nb_char += _puts(ptr);
return (nb_char);
}
/**
* print_hex - convert into hex and print
* @arg: parameter to convert
* @flag: flag parameter
* Return: number of printed char
*/
int print_hex(va_list arg, flag_t flag)
{
int nb_char = 0;
unsigned int num = va_arg(arg, unsigned int), base = num;
static const char number[] = "0123456789abcdef";
static char buffer[10];
char *ptr;
ptr = &buffer[9];
*ptr = '\0';
do {
*--ptr = number[num % 16];
num /= 16;
} while (num != 0);
if (flag.diese == 1 && base != 0)
nb_char += _puts("0x");
nb_char += _puts(ptr);
return (nb_char);
}
/**
* print_hex_maj - convert into hex and print
* @arg: parameter to convert
* @flag: flag parameter
* Return: number of printed char
*/
int print_hex_maj(va_list arg, flag_t flag)
{
int nb_char = 0;
unsigned int num = va_arg(arg, unsigned int), base = num;
static const char number[] = "0123456789ABCDEF";
static char buffer[10];
char *ptr;
ptr = &buffer[9];
*ptr = '\0';
do {
*--ptr = number[num % 16];
num /= 16;
} while (num != 0);
if (flag.diese == 1 && base != 0)
nb_char += _puts("0X");
nb_char += _puts(ptr);
return (nb_char);
}