-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_functions_derived.c
94 lines (79 loc) · 1.65 KB
/
print_functions_derived.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
#include "main.h"
#include <stdarg.h>
#include <stdlib.h>
/**
* print_unsignedToBinary - prints an integer.
* @arg: argument.
* Return: 0
*/
int print_unsignedToBinary(va_list arg)
{
unsigned int n = va_arg(arg, unsigned int);
unsigned int printed;
print_binary(n, &printed);
print_binary(n, &printed);
return (printed);
}
/**
* print_oct - prints number in octal base.
* @arg: list containing octal number to be printed.
* Return: number of octals printed
*/
int print_oct(va_list arg)
{
unsigned int num = va_arg(arg, unsigned int);
unsigned int copy;
char *octa;
int i, j, charPrinted = 0;
if (num == 0)
return (_putchar('0'));
for (copy = num; copy != 0; j++)
{
copy = copy / 8;
}
octa = malloc(j);
if (!octa)
return (-1);
for (i = j - 1; i >= 0; i--)
{
octa[i] = num % 8 + '0';
num = num / 8;
}
for (i = 0; i < j && octa[i] == '0'; i++)
;
for (; i < j; i++)
{
_putchar(octa[i]);
charPrinted++;
}
free(octa);
return (charPrinted);
}
/**
* print_unsignedIntToHex - prints unsigned int to hexadecimal.
* @num: number to print
* @_case: letter `a` on the case to print it (upper or lower)
* Return: number or char printed
*/
int print_unsignedIntToHex(unsigned int num, char _case)
{
unsigned int num2;
int i, j, remainder, nbrCharacters = 0;
char *numhex;
for (num2 = num; num2 != 0; nbrCharacters++, num2 /= 16)
;
numhex = malloc(nbrCharacters);
for (i = 0; num != 0; i++)
{
remainder = num % 16;
if (remainder < 10)
numhex[i] = remainder + '0';
else
numhex[i] = remainder - 10 + _case;
num = num / 16;
}
for (j = i - 1; j >= 0; j--)
_putchar(numhex[j]);
free(numhex);
return (nbrCharacters);
}