-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_hexa.c
53 lines (50 loc) · 1.43 KB
/
ft_hexa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbifenzi <mbifenzi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/31 14:08:39 by mbifenzi #+# #+# */
/* Updated: 2020/02/19 02:10:27 by mbifenzi ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ft_printf.h"
char *hexa(unsigned int i, char c)
{
unsigned int j;
unsigned int k;
int len;
char *s;
char a;
k = 0;
j = i;
a = (c == 'X') ? 'A' : 'a';
len = 0;
if (i == 0)
len = 1;
while (j > 0)
{
j = j / 16;
len++;
}
if(!(s = malloc(sizeof(char) * (len + 1))))
return(0);
s[len] = '\0';
len--;
while (len >= 0)
{
if (i % 16 >= 10)
s[len] = a + (i % 16) % 10;
else
s[len] = i % 16 +'0';
i = i/ 16;
len--;
}
return(s);
}