Skip to content

Commit 43c30b2

Browse files
committed
hex: adapt code to use chex primitives
1 parent 8b26fbd commit 43c30b2

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/hex/hex.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,30 @@
99
*/
1010

1111
#include "hex.h"
12+
#include "chex.h"
1213

1314
namespace arduino { namespace hex {
1415

15-
String encode(uint8_t* in, uint32_t size) {
16-
String out;
17-
out.reserve((size * 2) + 1);
16+
String encode(const uint8_t* in, uint32_t size) {
17+
char out[(size * 2) + 1];
18+
unsigned int byteNumber;
19+
byteNumber = chex_encode(out, sizeof(out), in, size);
20+
out[byteNumber] = 0;
21+
return String(out);
22+
}
23+
24+
String encodeUpper(const uint8_t* in, uint32_t size) {
25+
String out = encode(in, size);
26+
out.toUpperCase();
27+
return out;
28+
}
1829

19-
char *ptr = out.begin();
20-
for (uint32_t i = 0; i < size; i++) {
21-
ptr += sprintf(ptr, "%02X", in[i]);
22-
}
23-
return String(out.c_str());
30+
bool decode(const String in, uint8_t* out, uint32_t size) {
31+
unsigned int byteNumber;
32+
byteNumber = chex_decode(out, size, in.begin(), in.length());
33+
Serial.println(byteNumber);
34+
Serial.println(in.length());
35+
return byteNumber * 2 == in.length();
2436
}
2537

2638
}} // arduino::hex

src/hex/hex.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ namespace arduino { namespace hex {
1717
* This library contains the methods to get board provisioning id
1818
*/
1919

20-
String encode(uint8_t* in, uint32_t size);
20+
String encode(const uint8_t* in, uint32_t size);
21+
String encodeUpper(const uint8_t* in, uint32_t size);
22+
23+
bool decode(const String in, uint8_t* out, uint32_t size);
2124

2225
}} // arduino::hex

0 commit comments

Comments
 (0)