-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_ascii.c
75 lines (68 loc) · 1.85 KB
/
hex_ascii.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hex_ascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nnaumenk <nnaumenk@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/08 14:10:46 by nnaumenk #+# #+# */
/* Updated: 2018/11/15 23:36:50 by nnaumenk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ssl.h"
void ft_ascii_to_hex(char *hex, char *str, size_t len)
{
char const *base = "0123456789abcdef";
char *pointer;
unsigned char byte;
pointer = hex;
while (len--)
{
byte = (unsigned char)*str++;
pointer[1] = base[byte % 16];
byte /= 16;
pointer[0] = base[byte % 16];
pointer += 2;
}
}
int ft_hex_to_dec(uint8_t hex)
{
int dec;
if (IS_NUMBER(hex))
dec = (hex - '0');
else if (IS_UP_HEX(hex))
dec = (hex - 'A' + 10);
else if (IS_DOWN_HEX(hex))
dec = (hex - 'a' + 10);
else
{
ft_print_fd(2, "no-hex digit\n");
return (-1);
}
return (dec);
}
int ft_hex_to_ascii(uint8_t *bytes, char *hex, size_t len)
{
size_t i;
int dec;
i = -1;
while (++i < len)
{
*bytes = 0;
if (*hex == 0)
break ;
dec = ft_hex_to_dec(*hex++);
if (dec == -1)
return (1);
*bytes = 16 * dec;
if (*hex == 0)
break ;
dec = ft_hex_to_dec(*hex++);
if (dec == -1)
return (1);
*bytes++ += dec;
}
while (++i < len)
*++bytes = 0;
return (0);
}