Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active October 28, 2021 10:16
Embed
What would you like to do?
endian conversion
// requires C++23 or later
#include <bit>
#include <concepts>
template <std::integral T>
constexpr T hton(T value) noexcept
{
if constexpr (std::endian::native == std::endian::little) {
return std::byteswap(value);
} else {
return value;
}
}
template <std::integral T>
constexpr T ntoh(T value) noexcept
{
if constexpr (std::endian::native == std::endian::little) {
return std::byteswap(value);
} else {
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment