-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_hex.c
More file actions
79 lines (72 loc) · 2.09 KB
/
convert_hex.c
File metadata and controls
79 lines (72 loc) · 2.09 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* convert_hex.c :+: :+: */
/* +:+ */
/* By: nsterk <nsterk@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/12/14 13:04:53 by nsterk #+# #+# */
/* Updated: 2021/01/12 16:38:02 by nsterk ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int convert_lowhex(t_tab *tab)
{
char *temp;
tab->specifier = 'x';
tab->format++;
temp = ft_unsigned_itoa_base(get_unsigned(tab),
"0123456789abcdef");
if (!temp)
return (-1);
if (tab->hash && ft_strcmp("0", temp))
tab->argument = ft_strjoin("0x", temp);
else
{
tab->argument = ft_strdup(temp);
tab->hash = 0;
}
free(temp);
if (!tab->argument)
return (-1);
return (1);
}
int convert_uphex(t_tab *tab)
{
char *temp;
tab->specifier = 'X';
tab->format++;
temp = ft_unsigned_itoa_base(get_unsigned(tab),
"0123456789ABCDEF");
if (!temp)
return (-1);
if (tab->hash && ft_strcmp("0", temp))
tab->argument = ft_strjoin("0X", temp);
else
{
tab->argument = ft_strdup(temp);
tab->hash = 0;
}
free(temp);
if (!tab->argument)
return (-1);
return (1);
}
int convert_ptr(t_tab *tab)
{
char *str;
tab->specifier = 'p';
tab->format++;
str = ft_unsigned_itoa_base(va_arg(tab->args, unsigned long long),
"0123456789abcdef");
if (!str)
return (-1);
if (tab->precision_bool && !tab->precision && !ft_strcmp("0", str))
tab->argument = ft_strdup("0x");
else
tab->argument = ft_strjoin("0x", str);
free(str);
if (!tab->argument)
return (-1);
return (1);
}