-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_functions.cpp
More file actions
81 lines (73 loc) · 2.66 KB
/
Copy pathconvert_functions.cpp
File metadata and controls
81 lines (73 loc) · 2.66 KB
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
#include <cstdlib>
#include <cinttypes>
#include <cstdio>
int convert_hex_string_naive_sscanf(const char* input, char* output, const size_t in_length)
{
char buffer[] = "\0\0";
uint8_t byte;
size_t length = in_length / 2;
for (size_t idx = 0; idx < length; ++idx)
{
buffer[0] = *(input++);
buffer[1] = *(input++);
sscanf(buffer, "%02" SCNx8, &byte);
output[idx] = byte;
}
return length;
}
int convert_hex_string_naive_strtoul(const char* input, char* output, const size_t in_length)
{
char buffer[] = "\0\0";
size_t length = in_length / 2;
for (size_t idx = 0; idx < length; ++idx)
{
buffer[0] = *(input++);
buffer[1] = *(input++);
output[idx] = static_cast<uint8_t>(strtoul(buffer, nullptr, 16));
}
return length;
}
int convert_hex_string_naive_lookup(const char* input, char* output, const size_t length)
{
static const int8_t lookup[256] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
};
for (size_t idx = 0; idx < (length / 2); ++idx)
{
//output[idx] = 16 * lookup[*(input++)];
//output[idx] += lookup[*(input++)];
output[idx] = lookup[*(input++)] << 4;
output[idx] += lookup[*(input++)];
}
return length;
}
int convert_hex_string_calculated(const char* input, char* output, const size_t length)
{
auto hexd_nibble_to_value = [](const char c) -> uint8_t
{
return c <= '9' ? c - '0' : 0xA + (
c <= 'F' ? c - 'A' :
c - 'a');
};
auto hexd_byte_to_value = [&](const char hexd_byte[2]) -> uint8_t
{
return ( hexd_nibble_to_value(hexd_byte[0]) << 4 )
+ hexd_nibble_to_value(hexd_byte[1]);
};
for (size_t idx = 0; idx < (length / 2); ++idx)
{
output[idx] = hexd_byte_to_value(input);
input += 2;
}
return length;
}