forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHEXBuilder.ino
76 lines (58 loc) · 2.2 KB
/
HEXBuilder.ino
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
#include <HEXBuilder.h>
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("\n\n\nStart.");
// Convert a HEX string like 6c6c6f20576f726c64 to a binary buffer
//
{
const char * out = "Hello World";
const char * hexin = "48656c6c6f20576f726c6400"; // As the string above is \0 terminated too
unsigned char buff[256];
size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), hexin);
if (len != 1 + strlen(out))
Serial.println("Odd - length 1 is wrong");
if (memcmp(buff, out, len) != 0)
Serial.println("Odd - decode 1 went wrong");
// Safe to print this binary buffer -- as we've included a \0 in the hex sequence.
//
Serial.printf("IN: <%s>\nOUT <%s\\0>\n", hexin, buff);
};
{
String helloHEX = "48656c6c6f20576f726c64";
const char hello[] = "Hello World";
unsigned char buff[256];
size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), helloHEX);
if (len != strlen(hello))
Serial.println("Odd - length 2 is wrong");
if (strcmp((char *) buff, hello) != 0)
Serial.println("Odd - decode 2 went wrong");
}
{
const unsigned char helloBytes[] = { 0x48, 0x56, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };
String helloHEX = "48566c6c6f20576f726c64";
String out = HEXBuilder::bytes2hex(helloBytes, sizeof(helloBytes));
if (out.length() != 2 * sizeof(helloBytes))
Serial.println("Odd - length 3 is wrong");
// we need to ignore case - as a hex string can be spelled in uppercase and lowercase
//
if (!out.equalsIgnoreCase(helloHEX)) {
Serial.println("Odd - decode 3 went wrong");
}
}
{
const unsigned char helloBytes[] = { 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };
const char helloHex[] = "6c6c6f20576f726c64";
char buff[256];
size_t len = HEXBuilder::bytes2hex(buff, sizeof(buff), helloBytes, sizeof(helloBytes));
if (len != 1 + 2 * sizeof(helloBytes))
Serial.println("Odd - length 4 is wrong");
// we need to ignore case - as a hex string can be spelled in uppercase and lowercase
//
if (strcasecmp(buff, helloHex))
Serial.println("Odd - decode 4 went wrong");
}
Serial.println("Done.");
}
void loop() {
}