Small cleanup and utils

This commit is contained in:
momo5502
2025-01-18 19:36:12 +01:00
parent 4c5257098c
commit 9fc37fa3ef
2 changed files with 25 additions and 14 deletions

View File

@@ -79,6 +79,29 @@ namespace utils::string
return result;
}
template <typename Integer>
requires(std::is_integral_v<Integer>)
std::string to_hex_number(const Integer& i, const bool uppercase = false)
{
std::string res{};
res.reserve(sizeof(i) * 2);
const std::span data{reinterpret_cast<const std::byte*>(&i), sizeof(i)};
for (const auto value : data)
{
const auto [high, low] = to_hex(value, uppercase);
res.insert(res.begin(), {high, low});
}
while (res.size() > 1 && res.front() == '0')
{
res.erase(res.begin());
}
return res;
}
template <typename Integer>
requires(std::is_integral_v<Integer>)
std::string to_hex_string(const Integer& i, const bool uppercase = false)