-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_hexaconv.c
120 lines (110 loc) · 2.82 KB
/
ft_hexaconv.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexaconv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rledrin <rledrin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/13 10:15:42 by rledrin #+# #+# */
/* Updated: 2019/11/13 10:15:44 by rledrin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_swap(char *a, char *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}
int ft_dec2hex_up(unsigned int nb, char *str)
{
char *hex;
int i;
int j;
i = 0;
j = -1;
hex = (char[17]){"0123456789ABCDEF"};
if (nb == 0)
{
str[0] = '0';
str[1] = '\0';
return (1);
}
while (nb > 0)
{
str[i++] = hex[nb % 16];
nb /= 16;
}
str[i] = '\0';
i--;
while (j++ <= ((i / 2) - 1))
ft_swap(&(str[j]), &(str[i - j]));
return (i + 1);
}
int ft_dec2hex_low(unsigned int nb, char *str)
{
char *hex;
int i;
int j;
i = 0;
j = -1;
hex = (char[17]){"0123456789abcdef"};
if (nb == 0)
{
str[0] = '0';
str[1] = '\0';
return (1);
}
while (nb > 0)
{
str[i++] = hex[nb % 16];
nb /= 16;
}
str[i] = '\0';
i--;
while (j++ <= ((i / 2) - 1))
ft_swap(&(str[j]), &(str[i - j]));
return (i + 1);
}
void ft_print_hex2(t_tab *t, char *str, int len, int *variable)
{
if (t->sign == 0 && t->space == 1)
ft_putchar_fd_printf(t, ' ', 1);
*variable = 0;
if (t->min_len > t->precision && t->precision >= len)
*variable = t->min_len - (t->precision + t->sign);
else if (t->precision >= t->min_len && t->precision >= len)
*variable = 0;
else if (t->min_len > t->precision && len > t->precision)
*variable = t->min_len - len - t->sign;
if (t->precision > t->min_len)
{
ft_printchar(t, '0', (t->precision - len));
ft_putstr_fd_printf(t, str, 1);
}
}
void ft_print_hex(t_tab *t, char *str, int len)
{
int variable;
if (str[0] == '0' && t->precision == 0)
{
str[0] = '\0';
len--;
}
ft_print_hex2(t, str, len, &variable);
if (t->precision < 0 && t->zero == 1 && t->minus == 0)
{
ft_printchar(t, '0', (t->min_len - len - t->sign));
ft_putstr_fd_printf(t, str, 1);
}
else if (t->min_len >= t->precision)
{
if (t->minus == 0)
ft_printchar(t, ' ', (variable));
ft_printchar(t, '0', (t->precision - len));
ft_putstr_fd_printf(t, str, 1);
if (t->minus == 1)
ft_printchar(t, ' ', (variable));
}
}