-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinthex.c
More file actions
80 lines (71 loc) · 1.71 KB
/
Copy pathprinthex.c
File metadata and controls
80 lines (71 loc) · 1.71 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
80
/* ************************************************************************** */
/* */
/* :::::::: */
/* printhex.c :+: :+: */
/* +:+ */
/* By: jsmidt <jsmidt@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/10/28 12:37:33 by jsmidt #+# #+# */
/* Updated: 2025/10/31 13:39:14 by jsmidt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ilen(unsigned int n)
{
int i;
i = 1;
while (n / 16)
{
n = n / 16;
i++;
}
return (i);
}
static char *ft_itoahex(unsigned int n)
{
unsigned int m;
char *res;
int i;
m = n;
i = ilen(m);
res = malloc((i * sizeof(char) + 1));
if (!res)
return (NULL);
res[i] = '\0';
while (i)
{
if ((m % 16) < 10)
res[--i] = (m % 16) + '0';
if ((m % 16) >= 10)
res[--i] = (m % 16) + 'W';
m = m / 16;
}
return (res);
}
int ft_print_hexl(unsigned int n)
{
int len;
char *str;
str = ft_itoahex(n);
len = ft_strlen(str);
ft_putstr(str);
free(str);
return (len);
}
int ft_print_hexu(unsigned int n)
{
int len;
char *str;
int i;
str = ft_itoahex(n);
len = ft_strlen(str);
i = 0;
while (str[i])
{
str[i] = ft_toupper(str[i]);
i++;
}
ft_putstr(str);
free(str);
return (len);
}