chore: add string::fromHex
This commit is contained in:
@ -219,6 +219,33 @@ int fromHexChar(char c)
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
std::vector<uint8_t> fromHex(const std::string &hexString)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.reserve(hexString.size() / 2);
|
||||
|
||||
size_t i = 0;
|
||||
while (i < hexString.size()) {
|
||||
if (hexString[i] == ':') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i + 2 > hexString.size()) {
|
||||
return {}; // uneven character count follows, it's unclear how to interpret it
|
||||
}
|
||||
|
||||
auto high = fromHexChar(hexString[i]);
|
||||
auto low = fromHexChar(hexString[i + 1]);
|
||||
if (high < 0 || low < 0) {
|
||||
return {};
|
||||
}
|
||||
result.push_back(high * 16 + low);
|
||||
i += 2;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void uppercase(std::string &subject)
|
||||
{
|
||||
std::transform(subject.begin(), subject.end(), subject.begin(), ::toupper);
|
||||
|
||||
Reference in New Issue
Block a user