-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstring_to_bytes.cpp
More file actions
56 lines (46 loc) · 1.45 KB
/
string_to_bytes.cpp
File metadata and controls
56 lines (46 loc) · 1.45 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
#include <cstdint>
#include <cstring>
#include <string>
#include <memory>
#include <ByteConvert/ByteConvert.hpp>
int main()
{
// String that we will convert to bytes
std::string example_string = "facaceca";
// Array of bytes we expect to get
uint8_t expected[4] = {0xfa, 0xca, 0xce, 0xca};
// Convert string to bytes
size_t result_size = 0;
uint8_t* result = ByteConvert::hex_string_to_block(example_string, &result_size);
// Check result
if (result_size != 4) {
// Error, unexpected size of result
delete[] result;
return -1;
}
for (size_t i = 0; i < result_size; i++) {
if (expected[i] != result[i]) {
// Error, result not expected
delete[] result;
return -2;
}
}
// Clear result !!! It was allocated to heap so we need to manualy free it !!!!!!
delete[] result;
// Same as above, just implemented with smart pointers, which is safer
std::unique_ptr<uint8_t[]> result1(ByteConvert::hex_string_to_block(example_string, &result_size));
// Check result
if (result_size != 4) {
// Error, unexpected size of result
return -1;
}
for (size_t i = 0; i < result_size; i++) {
if (expected[i] != result1[i]) {
// Error, result not expected
return -2;
}
}
// No need to free result1, because it clears itself when it goes out of scope
// Success
return 0;
}