summaryrefslogtreecommitdiff
path: root/src/common/hex_util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/hex_util.h')
-rw-r--r--src/common/hex_util.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/common/hex_util.h b/src/common/hex_util.h
new file mode 100644
index 000000000..13d586015
--- /dev/null
+++ b/src/common/hex_util.h
@@ -0,0 +1,37 @@
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 <cstddef>
9#include <string>
10#include <fmt/format.h>
11#include "common/common_types.h"
12
13u8 ToHexNibble(char c1);
14
15template <size_t Size, bool le = false>
16std::array<u8, Size> HexStringToArray(std::string_view str) {
17 std::array<u8, Size> out{};
18 if constexpr (le) {
19 for (size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2)
20 out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
21 } else {
22 for (size_t i = 0; i < 2 * Size; i += 2)
23 out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
24 }
25 return out;
26}
27
28template <size_t Size>
29std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {
30 std::string out;
31 for (u8 c : array)
32 out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
33 return out;
34}
35
36std::array<u8, 0x10> operator"" _array16(const char* str, size_t len);
37std::array<u8, 0x20> operator"" _array32(const char* str, size_t len);