-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconv_hex.c
85 lines (76 loc) · 2.03 KB
/
conv_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
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* conv_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ydeineha <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/07 17:32:52 by ydeineha #+# #+# */
/* Updated: 2018/09/07 17:32:54 by ydeineha ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static char *str_itoa_base(unsigned char num,
unsigned char base, int len)
{
int i;
char *str;
i = len;
str = ft_strnew(len);
while (--i >= 0)
{
str[i] = HEX[num % base];
num = num / base;
}
return (str);
}
static unsigned int ft_atoi_base(char *str, long long base)
{
int i;
int len;
long long result;
i = 0;
result = 0;
len = ft_strlen(str);
while (ft_isspace(str[i]) == 1 && str[i] != '\0')
i++;
while (len > i)
{
result = (base * result) + ((ft_strchr(HEX, str[i]) - HEX));
i++;
}
if (result > 4294967295)
return (0);
return ((unsigned int)(result));
}
static char *hex_line(unsigned char *line, int length)
{
int i;
char *hex;
char *tmp;
i = 0;
hex = NULL;
tmp = NULL;
if (line[i] == '0')
i++;
hex = str_itoa_base(line[i], 16, 2);
while (++i < length)
{
tmp = str_itoa_base(line[i], 16, 2);
hex = ft_strljoin(&hex, &tmp);
}
return (hex);
}
unsigned int conv_hex(unsigned char *line, int length)
{
int i;
char *hex;
unsigned int ret;
i = 0;
hex = NULL;
if (!(hex = hex_line(line, length)))
return (0);
ret = ft_atoi_base(hex, 16);
ft_strdel(&hex);
return (ret);
}