-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.c
65 lines (60 loc) · 1.58 KB
/
conv.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* conv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 19:31:16 by bfrochot #+# #+# */
/* Updated: 2016/12/09 16:37:27 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
int ft_count(int len)
{
if (len < 8)
return (0);
else if (len < 12)
return (1);
else if (len < 17)
return (2);
else
return (3);
}
int plus(int i)
{
if (i == 3)
return (240);
else if (i == 2)
return (224);
else if (i == 1)
return (192);
else
return (0);
}
int conv(int nb, char *new)
{
char *nbr;
int len;
int k;
int i;
nbr = ft_itoa_base(nb, 2, 0);
len = ft_strlen(nbr);
if (len < 22)
{
k = 6;
i = ft_count(len);
while (i > 0)
{
new[i] = ft_atoi_base_printf(nbr + len - k, 2, 0) + 128;
nbr[len - k] = 0;
--i;
k += 6;
}
new[0] = ft_atoi_base_printf(nbr, 2, 0) + plus(k / 6 - 1);
}
else
return (-1);
free(nbr);
return (ft_count(len) + 1);
}