hex_util.h 1.1 KB

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