-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunsigned_type.c
More file actions
207 lines (196 loc) · 4.69 KB
/
Copy pathunsigned_type.c
File metadata and controls
207 lines (196 loc) · 4.69 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "holberton.h"
/**
* print_binary - prints binary version of unsigned int
* @input: input from argument list
* Return: number of characters printed
*/
int print_binary(va_list input)
{
unsigned int copy_input;
unsigned int result;
int index, power_x;
char *value_zero = "0";
char *value_one = "1";
copy_input = va_arg(input, unsigned int);
power_x = 0;
if (copy_input != 0)
{
result = copy_input - power(2, power_x);
while (result >= power(2, power_x + 1) - power(2, power_x))
{
power_x++;
result = copy_input - power(2, power_x);
}
}
result = copy_input;
for (index = 0; index <= power_x; index++)
{
if (result >= power(2, power_x - index))
{
write(1, value_one, sizeof(char));
result -= power(2, power_x - index);
}
else
write(1, value_zero, sizeof(char));
}
return (power_x + 1);
}
/**
* print_unsigned - prints unsigned int
* @input: input from variable argument list
* Return: number of characters printed
*/
int print_unsigned(va_list input)
{
unsigned int result;
char *storage;
int num_char;
result = va_arg(input, unsigned int);
storage = _u_itoa(result);
if (storage == NULL)
return (0);
num_char = 0;
while (storage[num_char] != '\0')
num_char++;
write(1, storage, sizeof(char) * num_char);
free(storage);
return (num_char);
}
/**
* print_hex_cap - prints an unsigned hexadecimal number
* @input: input from variable argument list
* Return: number of characters printed
*/
int print_hex_cap(va_list input)
{
unsigned long int copy_input;
unsigned long int result;
unsigned long int index, x;
char *hex_print = "0123456789ABCDEF";
unsigned long int base = 16;
unsigned long int multiple;
/* char *input_to_buf; */
copy_input = va_arg(input, unsigned int);
x = 0;
if (copy_input >= base)
{
result = copy_input - power(base, x);
while (result >= power(base, x + 1) - power(base, x))
{
x++;
result = copy_input - power(base, x);
}
}
result = copy_input;
/*
* input_to_buf = malloc(sizeof(char) * 2);
* if (input_to_buf == NULL)
* return (0);
* input_to_buf[1] = '\0';
*/
for (index = 0; index <= x; index++)
{
multiple = 1;
while (result >= multiple * power(base, x - index))
{
multiple++;
}
/* input_to_buf[0] = *(hex_values + (--multiple)); */
/* num_copied += buffer_copy(buf, input_to_buf, n + index); */
write(1, (hex_print + (--multiple)), sizeof(char));
result -= multiple * power(base, x - index);
}
return (x + 1);
}
/**
* print_hex_low - prints an unsigned hexadecimal number
* @input: input from variable argument list
* Return: number of characters printed
*/
int print_hex_low(va_list input)
{
unsigned long int copy_input;
unsigned long int result;
unsigned long int index, x;
char *hex_print = "0123456789abcdef";
unsigned long int base = 16;
unsigned long int multiple;
/*char *input_to_buf;*/
copy_input = va_arg(input, unsigned int);
x = 0;
if (copy_input >= base)
{
result = copy_input - power(base, x);
while (result >= power(base, x + 1) - power(base, x))
{
x++;
result = copy_input - power(base, x);
}
}
result = copy_input;
/*
* input_to_buf = malloc(sizeof(char) * 2);
* if (input_to_buf == NULL)
* return (0);
* input_to_buf[1] = '\0';
*/
for (index = 0; index <= x; index++)
{
multiple = 1;
while (result >= multiple * power(base, x - index))
{
multiple++;
}
/* input_to_buf[0] = *(hex_values + (--multiple)); */
/* num_copied += buffer_copy(buf, input_to_buf, n + index); */
write(1, (hex_print + (--multiple)), sizeof(char));
result -= multiple * power(base, x - index);
}
return (x + 1);
}
/**
* print_octal - prints an unsigned octal number
* @input: input from variable argument list
* Return: number of characters printed
*/
int print_octal(va_list input)
{
unsigned long int copy_input;
unsigned long int result;
unsigned long int index, x;
char *octal_values = "01234567";
unsigned long int base = 8;
unsigned long int multiple;
/*char *input_to_buf;*/
copy_input = va_arg(input, unsigned int);
x = 0;
if (copy_input >= base)
{
result = copy_input - power(base, x);
while (result >= power(base, x + 1) - power(base, x))
{
x++;
result = copy_input - power(base, x);
}
}
result = copy_input;
/*
* input_to_buf = malloc(sizeof(char) * 2);
* if (input_to_buf == NULL)
* return (0);
* input_to_buf[1] = '\0';
*/
for (index = 0; index <= x; index++)
{
multiple = 1;
while (result >= multiple * power(base, x - index))
{
multiple++;
}
/* input_to_buf[0] = *(hex_values + (--multiple)); */
/* num_copied += buffer_copy(buf, input_to_buf, n + index); */
write(1, (octal_values + (--multiple)), sizeof(char));
result -= multiple * power(base, x - index);
}
return (x + 1);
}