-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_binary.c
More file actions
166 lines (152 loc) · 3.02 KB
/
2_binary.c
File metadata and controls
166 lines (152 loc) · 3.02 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
#include "main.h"
#include <stdlib.h>
#include <stdarg.h>
/*#include <stdio.h>*/
/**
* _malloc - allocates suitable memory space for 'binary string'
* @num_list: list of arguments
* @base: conversion base
* Definition - determines how many bytes of memory should be
* allocated, and performs the memory allocation.
* Return: count of printed characters
*/
int _malloc(va_list num_list, int base)
{
int n;
unsigned int num;
char *buffer;
unsigned long int pow;
int i;
int count;
n = 0;
num = va_arg(num_list, unsigned int);
if (num == 0)
{
_putchar('0');
return (1);
}
while (1)
{
pow = 1;
for (i = 0; i < n; i++)
pow *= base;
if (pow <= num)
n++;
else
break;
}
buffer = malloc(sizeof(char) * n);
if (buffer == NULL)
return (0);
count = fill_buffer(buffer, num, n, base);
/*printf("Binary count: %d", count);*/
return (count);
}
/**
* _malloc2 - allocates suitable memory space for 'binary string'
* @num_list: list of arguments
* @base: conversion base
*
* Definition - determines how many bytes of memory should be
* allocated, and performs the memory allocation.
* Return: count of printed characters
*/
int _malloc2(va_list num_list, int base)
{
int n;
int num;
char *buffer;
long int pow;
int i;
int count;
long int num_long;
n = 0;
count = 0;
num = va_arg(num_list, int);
num_long = num;
/*printf("/NUm: %d/", num);*/
if (num == 0)
{
_putchar('0');
return (1);
}
if (num_long < 0)
{
_putchar('-');
num_long *= -1;
count++;
}
while (1)
{
pow = 1;
for (i = 0; i < n; i++)
pow *= base;
if (pow <= num_long)
n++;
else
break;
}
buffer = malloc(sizeof(char) * n);
if (buffer == NULL)
return (0);
count += fill_buffer(buffer, num_long, n, base);
/*printf("/Upper function with minus count: %d\n/", count);*/
return (count);
}
/**
* fill_buffer - fills up previously allocated memory space
* with binary, decimal, etc. digits
* @buffer: memory space to be filled up
* @num: number to be converted to binary
* @n: size of memory space
* @base: conversion base
*
* Definition - converts num to binary number and fills buffer
* Return: count of printed characters
*/
int fill_buffer(char *buffer, long int num, int n, int base)
{
int quotient;
int rem;
int i;
int count;
i = 0;
quotient = 1;
while (quotient != 0)
{
quotient = num / base;
/*printf("/Quotient: %d/", quotient);*/
rem = num % base;
/*printf("/remainder: %d/", rem);*/
buffer[i] = rem + '0';
num = quotient;
i++;
}
count = print_rev_buffer(buffer, n);
return (count);
}
/**
* print_rev_buffer - prints a reversed string
* @buffer: previously allocated memory space
* @n: buffer size
*
* Definition - prints reversed string; needed to print binary
* digits in the right order
* Return: number of characters printed
*/
int print_rev_buffer(char *buffer, int n)
{
int i;
int sub2_count;
sub2_count = 0;
i = n - 1;
while (i >= 0)
{
_putchar(buffer[i]);
i--;
sub2_count++;
}
free(buffer);
/*printf("/sub2_count: %d\n/", sub2_count);*/
return (sub2_count);
}