-
Notifications
You must be signed in to change notification settings - Fork 1
/
printhex.c
73 lines (69 loc) · 1.41 KB
/
printhex.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
#include "main.h"
/**
* printhex - prints a hexadecimal
* @format: format to print hexadecimal
* @pa: list that contains the hexadecimal
* Return: number.
*/
int printhex(char *format, va_list pa)
{
unsigned int num = va_arg(pa, unsigned int);
unsigned int num2;
int i, i2, copy, contame = 0;
char *numhex;
(void)format;
if (num == 0)
return (_putchar('0'));
for (num2 = num; num2 != 0; contame++)
{
num2 = num2 / 16;
}
numhex = malloc(contame);
for (i = 0; num != 0; i++)
{
copy = num % 16;
if (copy < 10)
numhex[i] = copy + '0';
else
numhex[i] = copy - 10 + 'a';
num = num / 16;
}
for (i2 = i - 1; i2 >= 0; i2--)
_putchar(numhex[i2]);
free(numhex);
return (contame);
}
/**
* printHEX - prints a hexadecimal
* @format: format to print hexadecimal
* @pa: va_list that contains the hexadecimal to print
* Return: number of digits printed
*/
int printHEX(char *format, va_list pa)
{
unsigned int NUM = va_arg(pa, unsigned int);
unsigned int NUM2;
int I, I2, COPY, CONTAME = 0;
char *NUMHEX;
(void)format;
if (NUM == 0)
return (_putchar('0'));
for (NUM2 = NUM; NUM2 != 0; CONTAME++)
{
NUM2 = NUM2 / 16;
}
NUMHEX = malloc(CONTAME);
for (I = 0; NUM != 0; I++)
{
COPY = NUM % 16;
if (COPY < 10)
NUMHEX[I] = COPY + '0';
else
NUMHEX[I] = COPY - 10 + 'A';
NUM = NUM / 16;
}
for (I2 = I - 1; I2 >= 0; I2--)
_putchar(NUMHEX[I2]);
free(NUMHEX);
return (CONTAME);
}