-
Notifications
You must be signed in to change notification settings - Fork 19
/
tohex.h
54 lines (45 loc) · 1.46 KB
/
tohex.h
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
#define HEX_PRELUDE \
const char *__hexm = "0123456789abcdef";
#define HEX_1_BYTE(buf, dest) \
{ \
char byte = (buf)[0]; \
(dest)[0] = __hexm[(byte >> 4) & 0x0F]; \
(dest)[1] = __hexm[byte & 0x0F]; \
}
#define HEX_2_BYTES(buf, dest) \
{ \
HEX_1_BYTE(buf, dest) \
HEX_1_BYTE(buf + 1, dest + 2) \
}
#define HEX_4_BYTES(buf, dest) \
{ \
HEX_2_BYTES(buf, dest) \
HEX_2_BYTES(buf + 2, dest + 4) \
}
#define HEX_8_BYTES(buf, dest) \
{ \
HEX_4_BYTES(buf, dest) \
HEX_4_BYTES(buf + 4, dest + 8) \
}
static inline void
uuid_to_str(const char *source, char *dest)
{
HEX_PRELUDE
HEX_4_BYTES(source, dest)
dest[8] = '-';
HEX_2_BYTES(source + 4, dest + 9)
dest[13] = '-';
HEX_2_BYTES(source + 6, dest + 14)
dest[18] = '-';
HEX_2_BYTES(source + 8, dest + 19)
dest[23] = '-';
HEX_4_BYTES(source + 10, dest + 24)
HEX_2_BYTES(source + 14, dest + 32)
}
static inline void
uuid_to_hex(const char *source, char *dest)
{
HEX_PRELUDE
HEX_8_BYTES(source, dest)
HEX_8_BYTES(source + 8, dest + 16)
}