summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/utils/cpp/hex_string.hpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/utils/cpp/hex_string.hpp b/src/utils/cpp/hex_string.hpp
index 86ea1b9e..b46240f8 100644
--- a/src/utils/cpp/hex_string.hpp
+++ b/src/utils/cpp/hex_string.hpp
@@ -2,6 +2,8 @@
#define INCLUDED_SRC_UTILS_CPP_HEX_STRING_HPP
#include <iomanip>
+#include <iostream>
+#include <optional>
#include <sstream>
#include <string>
@@ -16,4 +18,20 @@
return ss.str();
}
+[[nodiscard]] static inline auto FromHexString(std::string const& hexstring)
+ -> std::optional<std::string> {
+ try {
+ const size_t kHexBase = 16;
+ std::stringstream ss{};
+ for (size_t i = 0; i < hexstring.length(); i += 2) {
+ unsigned char c =
+ std::stoul(hexstring.substr(i, 2), nullptr, kHexBase);
+ ss << c;
+ }
+ return ss.str();
+ } catch (...) {
+ return std::nullopt;
+ }
+}
+
#endif // INCLUDED_SRC_UTILS_CPP_HEX_STRING_HPP