mirror of
https://github.com/yuzu-emu/yuzu.git
synced 2024-12-27 08:10:04 +00:00
hex_util: Add HexVectorToString and HexStringToVector
Converts between bytes and strings when the size is not known at compile time.
This commit is contained in:
parent
f85f2b3728
commit
f62227aa95
@ -18,6 +18,25 @@ u8 ToHexNibble(char c1) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) {
|
||||||
|
std::vector<u8> out(str.size() / 2);
|
||||||
|
if (little_endian) {
|
||||||
|
for (std::size_t i = str.size() - 2; i <= str.size(); i -= 2)
|
||||||
|
out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
|
||||||
|
} else {
|
||||||
|
for (std::size_t i = 0; i < str.size(); i += 2)
|
||||||
|
out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string HexVectorToString(std::vector<u8> vector, bool upper) {
|
||||||
|
std::string out;
|
||||||
|
for (u8 c : vector)
|
||||||
|
out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
std::array<u8, 16> operator""_array16(const char* str, std::size_t len) {
|
std::array<u8, 16> operator""_array16(const char* str, std::size_t len) {
|
||||||
if (len != 32) {
|
if (len != 32) {
|
||||||
LOG_ERROR(Common,
|
LOG_ERROR(Common,
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
|
||||||
@ -14,6 +15,8 @@ namespace Common {
|
|||||||
|
|
||||||
u8 ToHexNibble(char c1);
|
u8 ToHexNibble(char c1);
|
||||||
|
|
||||||
|
std::vector<u8> HexStringToVector(std::string_view str, bool little_endian);
|
||||||
|
|
||||||
template <std::size_t Size, bool le = false>
|
template <std::size_t Size, bool le = false>
|
||||||
std::array<u8, Size> HexStringToArray(std::string_view str) {
|
std::array<u8, Size> HexStringToArray(std::string_view str) {
|
||||||
std::array<u8, Size> out{};
|
std::array<u8, Size> out{};
|
||||||
@ -27,6 +30,8 @@ std::array<u8, Size> HexStringToArray(std::string_view str) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string HexVectorToString(std::vector<u8> vector, bool upper = true);
|
||||||
|
|
||||||
template <std::size_t Size>
|
template <std::size_t Size>
|
||||||
std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {
|
std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {
|
||||||
std::string out;
|
std::string out;
|
||||||
|
Loading…
Reference in New Issue
Block a user