-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_number.c
executable file
·106 lines (96 loc) · 2.28 KB
/
convert_number.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
#include "main.h"
/**
* print_hex - prints unsigned hex numbers in lowercase
* @ap: the argument pointer
* @params: the parameters struct
*
* Return: bytes printed
*/
int print_hex(va_list ap, params_t *params)
{
unsigned long l;
int c = 0;
char *str;
if (params->l_modifier)
l = (unsigned long)va_arg(ap, unsigned long);
else if (params->h_modifier)
l = (unsigned short int)va_arg(ap, unsigned int);
else
l = (unsigned int)va_arg(ap, unsigned int);
str = convert(l, 16, CONVERT_UNSIGNED | CONVERT_LOWERCASE, params);
if (params->hashtag_flag && l)
{
*--str = 'x';
*--str = '0';
}
params->unsign = 1;
return (c += print_number(str, params));
}
/**
* print_HEX - prints unsigned hex numbers in uppercase
* @ap: the argument pointer
* @params: the parameters struct
*
* Return: bytes printed
*/
int print_HEX(va_list ap, params_t *params)
{
unsigned long l;
int c = 0;
char *str;
if (params->l_modifier)
l = (unsigned long)va_arg(ap, unsigned long);
else if (params->h_modifier)
l = (unsigned short int)va_arg(ap, unsigned int);
else
l = (unsigned int)va_arg(ap, unsigned int);
str = convert(l, 16, CONVERT_UNSIGNED, params);
if (params->hashtag_flag && l)
{
*--str = 'X';
*--str = '0';
}
params->unsign = 1;
return (c += print_number(str, params));
}
/**
* print_binary - prints unsigned binary number
* @ap: the argument pointer
* @params: the parameters struct
*
* Return: bytes printed
*/
int print_binary(va_list ap, params_t *params)
{
unsigned int n = va_arg(ap, unsigned int);
char *str = convert(n, 2, CONVERT_UNSIGNED, params);
int c = 0;
if (params->hashtag_flag && n)
*--str = '0';
params->unsign = 1;
return (c += print_number(str, params));
}
/**
* print_octal - prints unsigned octal numbers
* @ap: the argument pointer
* @params: the parameters struct
*
* Return: bytes printed
*/
int print_octal(va_list ap, params_t *params)
{
unsigned long l;
char *str;
int c = 0;
if (params->l_modifier)
l = (unsigned long)va_arg(ap, unsigned long);
else if (params->h_modifier)
l = (unsigned short int)va_arg(ap, unsigned int);
else
l = (unsigned int)va_arg(ap, unsigned int);
str = convert(l, 8, CONVERT_UNSIGNED, params);
if (params->hashtag_flag && l)
*--str = '0';
params->unsign = 1;
return (c += print_number(str, params));
}