-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_uitox_plus.c
86 lines (79 loc) · 2.2 KB
/
ft_uitox_plus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_uitox_plus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yuske <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 14:56:07 by yuske #+# #+# */
/* Updated: 2023/01/13 12:11:09 by yuske ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t figure_counter_hex(unsigned int nbr, t_pft *pft);
static void uint_processor_hex(unsigned int nbr, t_pft *pft);
static char *ft_toupper_str(char *str);
//14L
void ft_uitox_plus(unsigned int nbr, t_pft *pft)
{
pft->figure = figure_counter_hex(nbr, pft);
pft->to_ascii = (char *)ft_calloc(pft->figure + 1, sizeof(char));
if (pft->to_ascii != NULL)
{
uint_processor_hex(nbr, pft);
if (pft->specifier == 'X')
ft_toupper_str(pft->to_ascii);
ft_putstr_plus(pft->to_ascii, pft);
free(pft->to_ascii);
}
}
//11L
static size_t figure_counter_hex(unsigned int nbr, t_pft *pft)
{
pft->i = 0;
if (nbr == 0)
pft->i = 1;
while (nbr > 0)
{
nbr /= 16;
pft->i += 1;
}
return (pft->i);
}
//24L
static void uint_processor_hex(unsigned int nbr, t_pft *pft)
{
size_t divider;
int to_c;
pft->i = 1;
divider = 1;
to_c = 0;
while (pft->i < pft->figure)
{
divider *= 16;
pft->i += 1;
}
pft->i = 0;
while (pft->i < pft->figure)
{
to_c = nbr / divider % 16;
if (to_c <= 9)
pft->to_ascii[pft->i] = to_c + '0';
else if (to_c > 9)
pft->to_ascii[pft->i] = to_c + ('a' - 10);
nbr %= divider;
divider /= 16;
pft->i += 1;
}
}
//7L
static char *ft_toupper_str(char *str)
{
while (*str != '\0')
{
if (*str >= 'a' && *str <= 'z')
*str += ('A' - 'a');
str += 1;
}
return (str);
}