-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-functions2.c
151 lines (135 loc) · 2.84 KB
/
8-functions2.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
#include "main.h"
/**
* input_long_oct - calculates a long octal number
*
* @bin: array where is stored the binary.
* @oct: array where is stored the octal.
*
* Return: binary array.
*/
char *input_long_oct(char *bin, char *oct)
{
int n, i, j, m, max_size;
oct[22] = '\0';
for (i = 63, m = 21; i >= 0; i--, m--)
{
if (i > 0)
max_size = 4;
else
max_size = 1;
for (n = 0, j = 1; j <= max_size; j *= 2, i--)
n = ((bin[i] - '0') * j) + n;
i++;
oct[m] = n + '0';
}
return (oct);
}
/**
* print_short_uhexa - Prints short hexadicimal
*
* @arg_list: Input hexadicimal argument (int)
* @buffer: Buffer array (char)
* @index: Number of buffer index (int)
*
* Return: The length of hexadicimal output
*/
int print_short_uhexa(va_list arg_list, char *buffer, unsigned int index)
{
short int number = va_arg(arg_list, int);
short int i, check_negative = 0, count, n;
char *hexa, *binary;
if (number == 0)
{
index = _puts(buffer, '0', index);
return (1);
}
if (number < 0)
{
number = (number * -1) - 1;
check_negative = 1;
}
binary = malloc(sizeof(char) * (16 + 1));
binary = input_to_binary(binary, number, check_negative, 16);
hexa = malloc(sizeof(char) * (4 + 1));
hexa = input_to_hexa(binary, hexa, 1, 4);
for (n = i = count = 0; hexa[i]; i++)
{
if (hexa[i] != '0' && n == 0)
n = 1;
if (n)
{
index = _puts(buffer, hexa[i], index);
count++;
}
}
free(binary);
free(hexa);
return (count);
}
/**
* input_short_oct - Calculates the short octal input
*
* @bin: Binary pointer (char)
* @oct: octal pointer (char)
*
* Return: Octal
*/
char *input_short_oct(char *bin, char *oct)
{
int n, i, j, m, max_size;
oct[6] = '\0';
for (i = 15, m = 5; i >= 0; i--, m--)
{
if (i > 0)
max_size = 4;
else
max_size = 1;
for (n = 0, j = 1; j <= max_size; j *= 2, i--)
n = ((bin[i] - '0') * j) + n;
i++;
oct[m] = n + '0';
}
return (oct);
}
/**
* print_long_uhexa - Prints long hexadicimal
*
* @arg_list: Input hexadicimal argument (int)
* @buffer: Buffer array (char)
* @index: Number of buffer index (int)
*
* Return: The length of hexadicimal output
*/
int print_long_uhexa(va_list arg_list, char *buffer, unsigned int index)
{
long int number = va_arg(arg_list, long int);
long int i, check_negative = 0, count, n;
char *hexa, *binary;
if (number == 0)
{
index = _puts(buffer, '0', index);
return (1);
}
if (number < 0)
{
number = (number * -1) - 1;
check_negative = 1;
}
binary = malloc(sizeof(char) * (64 + 1));
binary = input_to_binary(binary, number, check_negative, 64);
hexa = malloc(sizeof(char) * (16 + 1));
hexa = input_to_hexa(binary, hexa, 1, 16);
for (n = i = count = 0; hexa[i]; i++)
{
if (hexa[i] != '0' && n == 0)
n = 1;
if (n)
{
index = _puts(buffer, hexa[i], index);
count++;
}
}
free(binary);
free(hexa);
return (count);
}