diff options
Diffstat (limited to 'src/common/hex_util.h')
| -rw-r--r-- | src/common/hex_util.h | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/common/hex_util.h b/src/common/hex_util.h new file mode 100644 index 000000000..f16c58aab --- /dev/null +++ b/src/common/hex_util.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <fmt/format.h> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | u8 ToHexNibble(char c1); | ||
| 12 | |||
| 13 | template <size_t Size, bool le = false> | ||
| 14 | std::array<u8, Size> HexStringToArray(std::string_view str) { | ||
| 15 | std::array<u8, Size> out{}; | ||
| 16 | if constexpr (le) { | ||
| 17 | for (size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) | ||
| 18 | out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); | ||
| 19 | } else { | ||
| 20 | for (size_t i = 0; i < 2 * Size; i += 2) | ||
| 21 | out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); | ||
| 22 | } | ||
| 23 | return out; | ||
| 24 | } | ||
| 25 | |||
| 26 | template <size_t Size> | ||
| 27 | std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { | ||
| 28 | std::string out; | ||
| 29 | for (u8 c : array) | ||
| 30 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); | ||
| 31 | return out; | ||
| 32 | } | ||
| 33 | |||
| 34 | std::array<u8, 0x10> operator"" _array16(const char* str, size_t len); | ||
| 35 | std::array<u8, 0x20> operator"" _array32(const char* str, size_t len); | ||