From f62227aa95f96901b75670d3914971593f65f119 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 30 Sep 2018 18:13:02 -0400 Subject: hex_util: Add HexVectorToString and HexStringToVector Converts between bytes and strings when the size is not known at compile time. --- src/common/hex_util.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/common/hex_util.cpp') diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index 589ae5cbf..e516e5bbd 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp @@ -18,6 +18,25 @@ u8 ToHexNibble(char c1) { return 0; } +std::vector HexStringToVector(std::string_view str, bool little_endian) { + std::vector out(str.size() / 2); + if (little_endian) { + for (std::size_t i = str.size() - 2; i <= str.size(); i -= 2) + out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + } else { + for (std::size_t i = 0; i < str.size(); i += 2) + out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + } + return out; +} + +std::string HexVectorToString(std::vector vector, bool upper) { + std::string out; + for (u8 c : vector) + out += fmt::format(upper ? "{:02X}" : "{:02x}", c); + return out; +} + std::array operator""_array16(const char* str, std::size_t len) { if (len != 32) { LOG_ERROR(Common, -- cgit v1.2.3 From 70bd2bb1d3aec23fc052c9612f9e530c1a2de72d Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 1 Oct 2018 17:17:08 -0400 Subject: ips_layer: Deduplicate resource usage --- src/common/hex_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common/hex_util.cpp') diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index e516e5bbd..5b63f9e81 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp @@ -30,7 +30,7 @@ std::vector HexStringToVector(std::string_view str, bool little_endian) { return out; } -std::string HexVectorToString(std::vector vector, bool upper) { +std::string HexVectorToString(const std::vector& vector, bool upper) { std::string out; for (u8 c : vector) out += fmt::format(upper ? "{:02X}" : "{:02x}", c); -- cgit v1.2.3