-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_hex.c
54 lines (47 loc) · 1.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stales <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/13 18:29:05 by stales #+# #+# */
/* Updated: 2022/03/21 02:19:30 by stales ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char g_ht[0x10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
unsigned char *ft_hex_encode(unsigned char *dst, char *src, t_size size)
{
unsigned char *tmp;
tmp = dst;
while (--size)
{
*tmp++ = (unsigned char)g_ht[*src >> 0x4];
*tmp++ = (unsigned char)g_ht[*src++ & 0xF];
}
*tmp = 0;
return (dst);
}
unsigned char *ft_hex_decode(unsigned char *dst, char *src, t_size size)
{
unsigned char *tmp;
tmp = dst;
while (--size)
{
*tmp = ft_hex_nibble(*src++);
*tmp <<= 4;
*tmp++ |= ft_hex_nibble(*src++);
}
*tmp = 0;
return (dst);
}
unsigned char ft_hex_nibble(char c)
{
if (c >= '0' && c <= '9')
return (c & 0xF);
if (c >= 'a' && c <= 'f')
return (~ -(c & 0xF));
return (0);
}