-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_hex.c
81 lines (73 loc) · 1.85 KB
/
ft_hex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erigolon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 14:02:01 by erigolon #+# #+# */
/* Updated: 2023/01/19 09:45:03 by erigolon ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_strinvcpy(char *dst, char *src)
{
int i;
int len;
len = ft_strlen(src);
i = 0;
len--;
while (len >= 0)
{
dst[i] = src[len];
i++;
len--;
}
dst[i] = '\0';
}
char *newstr(char *str)
{
int len;
char *str_done;
len = ft_strlen(str);
str_done = (char *)malloc(len + 1);
if (!str_done)
return (0);
ft_strinvcpy(str_done, str);
return (str_done);
}
char *ft_hex(unsigned long decimal, int mayus)
{
unsigned int remainder;
int i;
char hex[100];
char *str;
i = 0;
if (decimal == 0)
hex[i++] = '0';
while (decimal != 0)
{
remainder = decimal % 16;
if (remainder < 10)
hex[i] = 48 + remainder;
else if (remainder >= 10 && mayus == 1)
hex[i] = 55 + remainder;
else if (remainder >= 10 && mayus == 0)
hex[i] = 87 + remainder;
i++;
decimal = decimal / 16;
}
hex[i] = '\0';
str = &hex[0];
str = newstr(str);
return (str);
}
/*
int main(void)
{
char *str;
str = ft_hex(0);
printf("%s", str);
return (0);
}
*/