-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_format_hex.c
More file actions
57 lines (52 loc) · 1.97 KB
/
ft_format_hex.c
File metadata and controls
57 lines (52 loc) · 1.97 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_format_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbalon-s <mbalon-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/28 20:35:33 by mbalon-s #+# #+# */
/* Updated: 2019/02/28 20:49:20 by mbalon-s ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static size_t format_hex_prepare_mem(t_specification spec, char *str)
{
size_t res;
ft_memset(str, ' ', spec.minwidth);
if (spec.force_zeroes && spec.align_left)
ft_memset(str, '0', spec.precision);
else if (spec.force_zeroes && !spec.precision_set)
ft_memset(str, '0', spec.minwidth);
else if (spec.force_zeroes && spec.precision != 0)
ft_memset(str + spec.minwidth - spec.precision, '0', spec.precision);
if (spec.align_left || (spec.force_zeroes && !spec.precision_set))
res = 0;
else
res = spec.minwidth - spec.precision;
return (res);
}
void ft_format_hex(unsigned long long int nbr,
t_specification spec,
char *str, char b)
{
size_t i;
size_t digits;
i = format_hex_prepare_mem(spec, str);
digits = spec.align_left ? spec.precision : spec.minwidth;
while (digits != i)
{
digits--;
str[digits] = (nbr & 0xF) + '0';
if (str[digits] > '9')
str[digits] = str[digits] - '0' - 10 + b;
nbr /= 16;
}
if (spec.alt_print)
{
if (spec.align_left)
digits = 0;
str[digits] = '0';
str[digits + 1] = b >= 'a' && b <= 'z' ? 'x' : 'X';
}
}