hex_util.h 1.4 KB

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