-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex_functions.c
75 lines (65 loc) · 1.24 KB
/
hex_functions.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
#include "main.h"
/**
* print_hexa - calculates the num of hexa digits and allocates memory
* @num: number to convert
* @counter: The pointer to store the count of hexa digits.
* Return: array of hex digits.
*/
int *print_hexa(unsigned int num, int *counter)
{
int x;
int *array;
unsigned int temp = num;
while (num / 16 != 0)
{
num /= 16;
(*counter)++;
}
(*counter)++;
array = malloc((*counter) * sizeof(int));
for (x = 0; x < (*counter); x++)
{
array[x] = temp % 16;
temp /= 16;
}
return (array);
}
/**
* print_hex - This prints a hexa num
* @val: The arguments passed
* Return: counter
*/
int print_hex(va_list val)
{
int counter = 0;
unsigned int num = va_arg(val, unsigned int);
int *array = print_hexa(num, &counter);
int x;
for (x = counter - 1; x >= 0; x--)
{
if (array[x] > 9)
array[x] += 7;
collab_putchar(array[x] + '0');
}
free(array);
return (counter);
}
/**
* print_hex_more - This prints a hexa num
* @num: The parameter
* Return: counter
*/
int print_hex_more(unsigned int num)
{
int counter = 0;
int *array = print_hexa(num, &counter);
int x;
for (x = counter - 1; x >= 0; x--)
{
if (array[x] > 9)
array[x] += 7;
collab_putchar(array[x] + '0');
}
free(array);
return (counter);
}