-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <format> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include "misc.h" | ||
|
||
namespace mtrx { | ||
namespace utils { | ||
|
||
std::string hexFromBytes(const uint8_t * bytes, const uint32_t size) { | ||
std::ostringstream oss; | ||
for (uint32_t i = 0; i < size; i++) | ||
oss << std::format("{:02x}", bytes[i]); | ||
return oss.str(); | ||
} | ||
|
||
bool hexToBytes(const std::string & hex, uint8_t * bytes, const uint32_t size) { | ||
if (hex.size() != size * 2) | ||
return false; | ||
|
||
for (auto c : hex) | ||
if (!std::isxdigit(c)) | ||
return false; | ||
|
||
for (uint32_t i = 0; i < size; i++) | ||
bytes[i] = std::stoul(hex.substr(i * 2, 2), nullptr, 16); | ||
|
||
return true; | ||
} | ||
|
||
}; // namespace utils | ||
}; // namespace mtrx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef INCLUDE_UTILS_MISC_H | ||
#define INCLUDE_UTILS_MISC_H | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
namespace mtrx { | ||
namespace utils { | ||
|
||
std::string hexFromBytes(const uint8_t * bytes, const uint32_t size); | ||
bool hexToBytes(const std::string & hex, uint8_t * bytes, const uint32_t size); | ||
|
||
}; // namespace utils | ||
}; // namespace mtrx | ||
|
||
#endif |