This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_printf_xtoa.c
73 lines (66 loc) · 1.73 KB
/
ft_printf_xtoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_xtoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <mcombeau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/17 18:02:35 by mcombeau #+# #+# */
/* Updated: 2022/01/17 18:02:42 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t ft_xtoa_len(long num)
{
size_t len;
len = 0;
if (num == 0)
return (1);
while (num >= 1)
{
len++;
num /= 16;
}
return (len);
}
static char *ft_hex_to_str(unsigned long int num, char *str, size_t len)
{
int mod;
str = ft_calloc(len + 1, sizeof(char));
if (str == NULL)
return (NULL);
len--;
while (len != (size_t)-1)
{
mod = num % 16;
if (mod < 10)
str[len] = mod + '0';
else if (mod >= 10)
str[len] = (mod - 10) + 'a';
num = num / 16;
len--;
}
return (str);
}
char *ft_printf_xtoa(unsigned long int num, int is_upper)
{
size_t len;
char *str;
int i;
len = ft_xtoa_len(num);
str = 0;
str = ft_hex_to_str(num, str, len);
if (!str)
return (NULL);
if (is_upper == 1)
{
i = 0;
while (str[i])
{
if (str[i] >= 'a' && str[i] <= 'f')
str[i] -= 32;
i++;
}
}
return (str);
}