Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active October 28, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohhoy/4bf969f7f093ceea0dd4f9622c8ca676 to your computer and use it in GitHub Desktop.
Save yohhoy/4bf969f7f093ceea0dd4f9622c8ca676 to your computer and use it in GitHub Desktop.
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