-
Notifications
You must be signed in to change notification settings - Fork 1
/
_putbases.c
80 lines (70 loc) · 1.25 KB
/
_putbases.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
#include "main.h"
/**
* _putbit - Prints an binary value to standard output
* @num: The unsigned number to be printed
*
* Return: Length of string printed - 1
*/
int _putbit(unsigned int num)
{
int len = 0;
unsigned int val;
if (!num)
{
len += _putchar('0');
return (len);
}
val = num % 2;
num = num / 2;
if (num)
len += _putbit(num);
len += _putchar(val + '0');
return (len);
}
/**
* _putoct - Prints an octal to standard output
* @num: The number to be printed
*
* Return: Length of string printed - 1
*/
int _putoct(unsigned int num)
{
int len = 0;
unsigned int val;
if (num < 8)
{
len += _putint(num);
return (len);
}
val = num % 8;
num = num / 8;
if (num)
len += _putoct(num);
len += _putchar(val + '0');
return (len);
}
/**
* _puthex - Prints an hex to standard output
* @num: The number to be printed
* @alpha: alpha value
* Return: Length of string printed - 1
*/
int _puthex(unsigned int num, char alpha)
{
int len = 0;
unsigned int val;
if (num < 10)
return (_putchar(num + '0'));
if (num < 16)
{
if (alpha == 'x')
return (_putchar(num + 87));
return (_putchar(num + 55));
}
val = num % 16;
num = num / 16;
if (num)
len += _puthex(num, alpha);
len += _puthex(val, alpha);
return (len);
}