forked from crankyoldgit/IRremoteESP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut_utils.h
35 lines (27 loc) · 886 Bytes
/
ut_utils.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
// Copyright 2022 Mateusz Bronk
#ifndef TEST_UT_UTILS_H_
#define TEST_UT_UTILS_H_
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
std::string bytesToHexString(const std::vector<uint8_t>& value) {
std::ostringstream oss;
oss << std::hex << std::setfill('0');
std::for_each(std::begin(value), std::end(value), [&oss] (uint8_t i) {
oss << std::setw(2) << std::uppercase << static_cast<uint16_t>(i);
});
return oss.str();
}
std::vector<uint8_t> hexStringToBytes(const std::string& hex) {
std::vector<uint8_t> bytes;
bytes.reserve(hex.length() / 2);
for (size_t i = 0; i < hex.length(); i += 2) {
std::string nextByte = hex.substr(i, 2);
uint8_t byte = static_cast<uint8_t>(strtol(nextByte.c_str(), nullptr, 16));
bytes.emplace_back(byte);
}
return bytes;
}
#endif // TEST_UT_UTILS_H_