-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_converters.c
148 lines (129 loc) · 3.08 KB
/
base_converters.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "main.h"
/**
* convert_bin - handler for string `%b` `%i`specifier
*
* @my_buffer: struct holding the final string and count
* @args: list of variable arguments
* @parsed_chars: pointer to variable tracking the chars parsed
* Return: (-1) if error, (0) otherwise
*/
int convert_bin(buf *my_buffer, va_list args, int *parsed_chars)
{
unsigned int value = va_arg(args, unsigned int);
char num[] = "01";
char hold[50];
char *ptr;
int i;
ptr = &hold[49];
*ptr = '\0';
do
{
i = value % 2;
value = value / 2;
*--ptr = num[i];
} while (value != 0); /*converting value to base 2 and turn*/
/* the number to string*/
while (*ptr) /*sending ptr to buffer so it can be printed*/
{
update_buff(my_buffer, *ptr);
ptr++;
}
(*parsed_chars) += 1;
return (0);
}
/**
* convert_oct - handler for string `%o` `%i`specifier
*
* @my_buffer: struct holding the final string and count
* @args: list of variable arguments
* @parsed_chars: pointer to variable tracking the chars parsed
* Return: (-1) if error, (0) otherwise
*/
int convert_oct(buf *my_buffer, va_list args, int *parsed_chars)
{
unsigned int value = va_arg(args, unsigned int);
char num[] = "0123456789";
char hold[50];
char *ptr;
int i;
ptr = &hold[49];
*ptr = '\0';
do
{
i = value % 8;
value = value / 8;
*--ptr = num[i];
} while (value != 0); /*converting value to base 2 and turn*/
/* the number to string*/
while (*ptr) /*sending ptr to buffer so it can be printed*/
{
update_buff(my_buffer, *ptr);
ptr++;
}
(*parsed_chars) += 1;
return (0);
}
/**
* convert_hex - handler for string `%x` `%i`specifier
*
* @my_buffer: struct holding the final string and count
* @args: list of variable arguments
* @parsed_chars: pointer to variable tracking the chars parsed
* Return: (-1) if error, (0) otherwise
*/
int convert_hex(buf *my_buffer, va_list args, int *parsed_chars)
{
unsigned int value = va_arg(args, unsigned int);
char num[] = "0123456789abcdef";
char hold[50];
char *ptr;
int i;
ptr = &hold[49];
*ptr = '\0';
do
{
i = value % 16;
value = value / 16;
*--ptr = num[i];
} while (value != 0); /*converting value to base 2 and turn*/
/* the number to string*/
while (*ptr) /*sending ptr to buffer so it can be printed*/
{
update_buff(my_buffer, *ptr);
ptr++;
}
(*parsed_chars) += 1;
return (0);
}
/**
* convert_hexa - handler for string `%X` `%i`specifier
*
* @my_buffer: struct holding the final string and count
* @args: list of variable arguments
* @parsed_chars: pointer to variable tracking the chars parsed
* Return: (-1) if error, (0) otherwise
*/
int convert_hexa(buf *my_buffer, va_list args, int *parsed_chars)
{
unsigned int value = va_arg(args, unsigned int);
char num[] = "0123456789ABCDEF";
char hold[50];
char *ptr;
int i;
ptr = &hold[49];
*ptr = '\0';
do
{
i = value % 16;
value = value / 16;
*--ptr = num[i];
} while (value != 0); /*converting value to base 2 and turn*/
/* the number to string*/
while (*ptr) /*sending ptr to buffer so it can be printed*/
{
update_buff(my_buffer, *ptr);
ptr++;
}
(*parsed_chars) += 1;
return (0);
}