hex_util.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include <string>
  8. #include <fmt/format.h>
  9. #include "common/common_types.h"
  10. u8 ToHexNibble(char c1);
  11. template <size_t Size, bool le = false>
  12. std::array<u8, Size> HexStringToArray(std::string_view str) {
  13. std::array<u8, Size> out{};
  14. if constexpr (le) {
  15. for (size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2)
  16. out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
  17. } else {
  18. for (size_t i = 0; i < 2 * Size; i += 2)
  19. out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
  20. }
  21. return out;
  22. }
  23. template <size_t Size>
  24. std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {
  25. std::string out;
  26. for (u8 c : array)
  27. out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
  28. return out;
  29. }
  30. std::array<u8, 0x10> operator"" _array16(const char* str, size_t len);
  31. std::array<u8, 0x20> operator"" _array32(const char* str, size_t len);