Last active
October 28, 2021 10:16
-
-
Save yohhoy/4bf969f7f093ceea0dd4f9622c8ca676 to your computer and use it in GitHub Desktop.
endian conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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