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 +++++++++++++++++++ src/common/hex_util.h | 5 +++++ 2 files changed, 24 insertions(+) (limited to 'src/common') 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, diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 863a5ccd9..0b7d3592a 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "common/common_types.h" @@ -14,6 +15,8 @@ namespace Common { u8 ToHexNibble(char c1); +std::vector HexStringToVector(std::string_view str, bool little_endian); + template std::array HexStringToArray(std::string_view str) { std::array out{}; @@ -27,6 +30,8 @@ std::array HexStringToArray(std::string_view str) { return out; } +std::string HexVectorToString(std::vector vector, bool upper = true); + template std::string HexArrayToString(std::array array, bool upper = true) { std::string out; -- 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 +- src/common/hex_util.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/common') 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); diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 0b7d3592a..68f003cb6 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -30,7 +30,7 @@ std::array HexStringToArray(std::string_view str) { return out; } -std::string HexVectorToString(std::vector vector, bool upper = true); +std::string HexVectorToString(const std::vector& vector, bool upper = true); template std::string HexArrayToString(std::array array, bool upper = true) { -- cgit v1.2.3