C++20 Endianness conversion library
The easiest way is to use built-in CMake FetchContent:
include(FetchContent)
FetchContent_Declare(ecpp_endian URL https://github.com/jmacheta/endian/tarball/latest)
FetchContent_MakeAvailable(ecpp_endian)
Then add a dependency to your target:
target_link_libraries(my_target PUBLIC ecpp::endian)
#include <ecpp/endian.hpp>
uint64_t a = 0x1122'3344'5566'7788U;
static_assert(0x8877'6655'4433'2211U == ecpp::byteswap(a));
// On little endian systems:
uint64_t a = 0x1122'3344'5566'7788U;
static_assert(0x8877'6655'4433'2211U == ecpp::hton(a));
static_assert(a == ecpp::ntoh(ecpp::hton(a)));
// On big endian systems:
uint64_t a = 0x1122'3344'5566'7788U;
static_assert(a == ecpp::hton(a));
static_assert(a == ecpp::ntoh(ecpp::hton(a)));
While every function is marked as constexpr
, in C++ standards prior to C++20, this might be untrue due to the non-constexpr std::memcpy
. I have checked many clang/gcc versions, and this does not seem to be an issue, but it's worth noting.