-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_changes.c
193 lines (175 loc) · 3.57 KB
/
base_changes.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
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
#include "main.h"
#include <stdio.h>
int hex_check(int, char);
/**
* print_binary - Converts a number from base 10 to binary
* @list: List of arguments passed to this function
* Return: The length of the number printed
*/
int print_binary(va_list list)
{
unsigned int num;
int i, len;
char *str;
char *rev_str;
num = va_arg(list, unsigned int);
if (num == 0)
return (_write_char('0'));
if (num < 1)
return (-1);
len = base_len(num, 2);
str = malloc(sizeof(char) * len + 1);
if (str == NULL)
return (-1);
for (i = 0; num > 0; i++)
{
if (num % 2 == 0)
str[i] = '0';
else
str[i] = '1';
num = num / 2;
}
str[i] = '\0';
rev_str = rev_string(str);
if (rev_str == NULL)
return (-1);
write_base(rev_str);
free(str);
free(rev_str);
return (len);
}
/**
* print_octal - Prints the numeric representation of a number in octal base
* @list: List of all the arguments passed to the program
* Return: Number of symbols printed to stdout
*/
int print_octal(va_list list)
{
unsigned int num;
int len;
char *octal_rep;
char *rev_str;
num = va_arg(list, unsigned int);
if (num == 0)
return (_write_char('0'));
if (num < 1)
return (-1);
len = base_len(num, 8);
octal_rep = malloc(sizeof(char) * len + 1);
if (octal_rep == NULL)
return (-1);
for (len = 0; num > 0; len++)
{
octal_rep[len] = (num % 8) + 48;
num = num / 8;
}
octal_rep[len] = '\0';
rev_str = rev_string(octal_rep);
if (rev_str == NULL)
return (-1);
write_base(rev_str);
free(octal_rep);
free(rev_str);
return (len);
}
/**
* print_hex - Prints a representation of a decimal number on base16 lowercase
* @list: List of the arguments passed to the function
* Return: Number of characters printed
*/
int print_hex(va_list list)
{
unsigned int num;
int len;
int rem_num;
char *hex_rep;
char *rev_hex;
num = va_arg(list, unsigned int);
if (num == 0)
return (_write_char('0'));
if (num < 1)
return (-1);
len = base_len(num, 16);
hex_rep = malloc(sizeof(char) * len + 1);
if (hex_rep == NULL)
return (-1);
for (len = 0; num > 0; len++)
{
rem_num = num % 16;
if (rem_num > 9)
{
rem_num = hex_check(rem_num, 'x');
hex_rep[len] = rem_num;
}
else
hex_rep[len] = rem_num + 48;
num = num / 16;
}
hex_rep[len] = '\0';
rev_hex = rev_string(hex_rep);
if (rev_hex == NULL)
return (-1);
write_base(rev_hex);
free(hex_rep);
free(rev_hex);
return (len);
}
/**
* print_heX - Prints a representation of a decimal number on base16 Uppercase
* @list: List of the arguments passed to the function
* Return: Number of characters printed
*/
int print_heX(va_list list)
{
unsigned int num;
int len;
int rem_num;
char *hex_rep;
char *rev_hex;
num = va_arg(list, unsigned int);
if (num == 0)
return (_write_char('0'));
if (num < 1)
return (-1);
len = base_len(num, 16);
hex_rep = malloc(sizeof(char) * len + 1);
if (hex_rep == NULL)
return (-1);
for (len = 0; num > 0; len++)
{
rem_num = num % 16;
if (rem_num > 9)
{
rem_num = hex_check(rem_num, 'X');
hex_rep[len] = rem_num;
}
else
hex_rep[len] = rem_num + 48;
num = num / 16;
}
hex_rep[len] = '\0';
rev_hex = rev_string(hex_rep);
if (rev_hex == NULL)
return (-1);
write_base(rev_hex);
free(hex_rep);
free(rev_hex);
return (len);
}
/**
* hex_check - Checks which hex function is calling it
* @num: Number to convert into letter
* @x: Tells which hex function is calling it
* Return: Ascii value for a letter
*/
int hex_check(int num, char x)
{
char *hex = "abcdef";
char *Hex = "ABCDEF";
num = num - 10;
if (x == 'x')
return (hex[num]);
else
return (Hex[num]);
return (0);
}