-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_hex.c
82 lines (69 loc) · 2.23 KB
/
convert_hex.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
#include "main.h"
unsigned int convert_x(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
unsigned int convert_X(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len);
/**
* convert_x - Converts an unsigned int argument to hex using abcdef
* and stores it to a buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_x(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
unsigned long int num;
unsigned int ret = 0;
char *lead = "0x";
if (len == LONG)
num = va_arg(args, unsigned long int);
else
num = va_arg(args, unsigned int);
if (len == SHORT)
num = (unsigned short)num;
if (HASH_FLAG == 1 && num != 0)
ret += _memcpy(output, lead, 2);
if (!(num == 0 && prec == 0))
ret += convert_ubase(output, num, "0123456789abcdef",
flags, wid, prec);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}
/**
* convert_X - Converts an unsigned int argument to hex using ABCDEF
* and stores it to a buffer contained in a struct.
* @args: A va_list pointing to the argument to be converted.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
* @len: A length modifier.
* @output: A buffer_t struct containing a character array.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_X(va_list args, buffer_t *output,
unsigned char flags, int wid, int prec, unsigned char len)
{
unsigned long int num;
unsigned int ret = 0;
char *lead = "0X";
if (len == LONG)
num = va_arg(args, unsigned long);
else
num = va_arg(args, unsigned int);
if (len == SHORT)
num = (unsigned short)num;
if (HASH_FLAG == 1 && num != 0)
ret += _memcpy(output, lead, 2);
if (!(num == 0 && prec == 0))
ret += convert_ubase(output, num, "0123456789ABCDEF",
flags, wid, prec);
ret += print_neg_width(output, ret, flags, wid);
return (ret);
}