diff options
| author | 2018-09-30 18:13:02 -0400 | |
|---|---|---|
| committer | 2018-10-04 11:32:04 -0400 | |
| commit | f62227aa95f96901b75670d3914971593f65f119 (patch) | |
| tree | 8c667ca88014a0764412993f68fd5b11c93036e8 | |
| parent | Merge pull request #1415 from DarkLordZach/ips (diff) | |
| download | yuzu-f62227aa95f96901b75670d3914971593f65f119.tar.gz yuzu-f62227aa95f96901b75670d3914971593f65f119.tar.xz yuzu-f62227aa95f96901b75670d3914971593f65f119.zip | |
hex_util: Add HexVectorToString and HexStringToVector
Converts between bytes and strings when the size is not known at compile time.
| -rw-r--r-- | src/common/hex_util.cpp | 19 | ||||
| -rw-r--r-- | src/common/hex_util.h | 5 |
2 files changed, 24 insertions, 0 deletions
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) { | |||
| 18 | return 0; | 18 | return 0; |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) { | ||
| 22 | std::vector<u8> out(str.size() / 2); | ||
| 23 | if (little_endian) { | ||
| 24 | for (std::size_t i = str.size() - 2; i <= str.size(); i -= 2) | ||
| 25 | out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); | ||
| 26 | } else { | ||
| 27 | for (std::size_t i = 0; i < str.size(); i += 2) | ||
| 28 | out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); | ||
| 29 | } | ||
| 30 | return out; | ||
| 31 | } | ||
| 32 | |||
| 33 | std::string HexVectorToString(std::vector<u8> vector, bool upper) { | ||
| 34 | std::string out; | ||
| 35 | for (u8 c : vector) | ||
| 36 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); | ||
| 37 | return out; | ||
| 38 | } | ||
| 39 | |||
| 21 | std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { | 40 | std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { |
| 22 | if (len != 32) { | 41 | if (len != 32) { |
| 23 | LOG_ERROR(Common, | 42 | 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 @@ | |||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <cstddef> | 8 | #include <cstddef> |
| 9 | #include <string> | 9 | #include <string> |
| 10 | #include <vector> | ||
| 10 | #include <fmt/format.h> | 11 | #include <fmt/format.h> |
| 11 | #include "common/common_types.h" | 12 | #include "common/common_types.h" |
| 12 | 13 | ||
| @@ -14,6 +15,8 @@ namespace Common { | |||
| 14 | 15 | ||
| 15 | u8 ToHexNibble(char c1); | 16 | u8 ToHexNibble(char c1); |
| 16 | 17 | ||
| 18 | std::vector<u8> HexStringToVector(std::string_view str, bool little_endian); | ||
| 19 | |||
| 17 | template <std::size_t Size, bool le = false> | 20 | template <std::size_t Size, bool le = false> |
| 18 | std::array<u8, Size> HexStringToArray(std::string_view str) { | 21 | std::array<u8, Size> HexStringToArray(std::string_view str) { |
| 19 | std::array<u8, Size> out{}; | 22 | std::array<u8, Size> out{}; |
| @@ -27,6 +30,8 @@ std::array<u8, Size> HexStringToArray(std::string_view str) { | |||
| 27 | return out; | 30 | return out; |
| 28 | } | 31 | } |
| 29 | 32 | ||
| 33 | std::string HexVectorToString(std::vector<u8> vector, bool upper = true); | ||
| 34 | |||
| 30 | template <std::size_t Size> | 35 | template <std::size_t Size> |
| 31 | std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { | 36 | std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { |
| 32 | std::string out; | 37 | std::string out; |