-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandle_hexadecimals.c
More file actions
128 lines (112 loc) · 3.14 KB
/
handle_hexadecimals.c
File metadata and controls
128 lines (112 loc) · 3.14 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "main.h"
#include <stdio.h>
static void get_hex_upper(char *hex_str);
/**
* handle_hex_lower - convert and append an integer in lower hexadecimal
* format
* @spec: format specifier information (unused)
* @args: arguments list
* @buffer: string buffer to store the result
*
* Return: the number of characters appended to the string buffer
*/
int handle_hex_lower(const format_specifier *spec, va_list args,
string_buffer *buffer)
{
char hex_str[17];
size_t n;
int characters_added, len;
size_t initial_length = buffer->length;
format_specifier *tmp_spec = (format_specifier *)spec;
if (spec->length == 'l')
n = va_arg(args, unsigned long int);
else
n = va_arg(args, unsigned int);
if (spec->precision)
{
tmp_spec->zero_flag = 1; /* handle precision for unsigned integers */
tmp_spec->width = spec->precision;
}
utob(n, hex_str, HEX);
len = _strlen(hex_str);
if (spec->sharp_flag && n > 0)
{
tmp_spec->width -= 2; /* account for the extra characters */
append_string(buffer, "0x");
}
if (spec->zero_flag)
handle_width(tmp_spec, buffer, len);
if (spec->width && spec->minus_flag == 0)
handle_width(tmp_spec, buffer, len);
if (!(n == 0 && !spec->precision))
append_string(buffer, hex_str); /* handle precision edge cases first*/
else
append_string(buffer, hex_str); /* append hex digits */
if (spec->minus_flag)
handle_width(tmp_spec, buffer, len);
characters_added = buffer->length - initial_length;
return (characters_added);
}
/**
* handle_hex_upper - convert and append an integer in uppercase hexadecimal
* format
* @spec: format specifier information (unused)
* @args: arguments list
* @buffer: string buffer to store the result
*
* Return: the number of characters appended to the string buffer
*/
int handle_hex_upper(const format_specifier *spec, va_list args,
string_buffer *buffer)
{
int len;
size_t n;
char hex_str[17];
int characters_added;
size_t initial_length = buffer->length;
format_specifier *tmp_spec = (format_specifier *)spec;
if (spec->length == 'l')
n = va_arg(args, unsigned long int);
else
n = va_arg(args, unsigned int);
if (spec->precision)
{
tmp_spec->zero_flag = 1; /* handle precision for unsigned integers */
tmp_spec->width = spec->precision;
}
utob(n, hex_str, HEX);
len = _strlen(hex_str);
get_hex_upper(hex_str); /* get the uppercase version of hex digit */
if (spec->sharp_flag && n > 0)
{
tmp_spec->width -= 2;
append_string(buffer, "0X");
}
if (spec->zero_flag)
handle_width(tmp_spec, buffer, len);
if (spec->width && spec->minus_flag == 0)
handle_width(tmp_spec, buffer, len);
if (!(n == 0 && !spec->precision))
append_string(buffer, hex_str);
else
append_string(buffer, hex_str); /* append hex digits */
if (spec->minus_flag)
handle_width(tmp_spec, buffer, len);
characters_added = buffer->length - initial_length;
return (characters_added);
}
/**
* get_hex_upper - updates a hex digit to its uppercase version
* @hex_str: the hex string to convert
*/
void get_hex_upper(char *hex_str)
{
int i;
for (i = 0; hex_str[i] != '\0'; i++)
{
if (hex_str[i] >= 'a' && hex_str[i] <= 'f')
{
hex_str[i] = hex_str[i] - 'a' + 'A';
}
}
}