-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_converter.c
52 lines (47 loc) · 918 Bytes
/
base_converter.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
#include "main.h"
/**
* convert_b - convert argument to binary
* and put it in the output
*
* @args: list of arguments
* @output: the result output
*
* Return: the number of bytes stored in buffer
*/
unsigned int convert_b(va_list args, buffer_t *output)
{
int d;
unsigned int len = 0;
char minus = '-';
d = va_arg(args, int);
if (d < 0)
{
len += _memcpy(output, &minus, 1);
d = -d;
}
len += convert_base(d, 2, "01", output);
return (len);
}
/**
* convert_o - convert argument to base8
* and put it in the output
*
* @args: list of arguments
* @output: the result output
*
* Return: the number of bytes stored in buffer
*/
unsigned int convert_o(va_list args, buffer_t *output)
{
int d;
unsigned int len = 0;
char minus = '-';
d = va_arg(args, int);
if (d < 0)
{
len += _memcpy(output, &minus, 1);
d = -d;
}
len += convert_base(d, 8, "01234567", output);
return (len);
}