Byte Conversions
Description
The library provides functions for converting safe integer types to and from big-endian or little-endian byte order.
These operate on the non-bounded unsigned types (u8, u16, u32, u64, u128) and their verified counterparts.
For big-endian conversions (to_be/from_be): on big-endian platforms these are no-ops; on little-endian platforms they delegate to byteswap.
For little-endian conversions (to_le/from_le): on little-endian platforms these are no-ops; on big-endian platforms they delegate to byteswap.
#include <boost/safe_numbers/byte_conversions.hpp>
to_be
Converts a value from the native byte order to big-endian byte order.
Runtime Overload
template <non_bounded_integral_library_type T>
requires (!is_verified_type_v<T>)
constexpr auto to_be(const T value) noexcept -> T;
Verified Overload
template <non_bounded_integral_library_type T>
consteval auto to_be(const verified_type_basis<T> value) noexcept -> verified_type_basis<T>;
Compile-time only overload for verified types.
Since to_be is consteval for verified types, the result is guaranteed to be a compile-time constant.
from_be
Converts a value from big-endian byte order to the native byte order.
This is the inverse of to_be.
Since byte swapping is its own inverse, from_be delegates directly to to_be.
Runtime Overload
template <non_bounded_integral_library_type T>
requires (!is_verified_type_v<T>)
constexpr auto from_be(const T value) noexcept -> T;
Verified Overload
template <non_bounded_integral_library_type T>
consteval auto from_be(const verified_type_basis<T> value) noexcept -> verified_type_basis<T>;
Compile-time only overload for verified types.
Since from_be is consteval for verified types, the result is guaranteed to be a compile-time constant.
to_le
Converts a value from the native byte order to little-endian byte order.
Runtime Overload
template <non_bounded_integral_library_type T>
requires (!is_verified_type_v<T>)
constexpr auto to_le(const T value) noexcept -> T;
Verified Overload
template <non_bounded_integral_library_type T>
consteval auto to_le(const verified_type_basis<T> value) noexcept -> verified_type_basis<T>;
Compile-time only overload for verified types.
Since to_le is consteval for verified types, the result is guaranteed to be a compile-time constant.
from_le
Converts a value from little-endian byte order to the native byte order.
This is the inverse of to_le.
Since byte swapping is its own inverse, from_le delegates directly to to_le.
Runtime Overload
template <non_bounded_integral_library_type T>
requires (!is_verified_type_v<T>)
constexpr auto from_le(const T value) noexcept -> T;
Verified Overload
template <non_bounded_integral_library_type T>
consteval auto from_le(const verified_type_basis<T> value) noexcept -> verified_type_basis<T>;
Compile-time only overload for verified types.
Since from_le is consteval for verified types, the result is guaranteed to be a compile-time constant.
to_be_bytes
Converts a safe integer value to a big-endian byte array.
The value is first converted to big-endian byte order using to_be, then reinterpreted as an array of std::byte.
Runtime Overload
template <non_bounded_integral_library_type T>
requires (!is_verified_type_v<T>)
constexpr auto to_be_bytes(const T value) noexcept -> std::array<std::byte, sizeof(T)>;
from_be_bytes
Reconstructs a safe integer value from a big-endian byte array.
The bytes are reinterpreted as the underlying type and then converted from big-endian to native byte order using from_be.
template <non_bounded_integral_library_type T, std::size_t N>
constexpr auto from_be_bytes(const std::span<const std::byte, N> bytes) -> T;
Parameters
-
bytes— A span of bytes in big-endian order. May have a fixed extent orstd::dynamic_extent.
Extent Matching
The function validates that the number of input bytes matches sizeof(T):
-
Fixed extent matches
sizeof(T): Compiles and executes normally. -
Fixed extent does not match
sizeof(T): Produces astatic_assertfailure at compile time. -
Dynamic extent: Checks at runtime and throws
std::domain_errorif the sizes do not match.
Example
using namespace boost::safe_numbers;
// From a fixed-extent span
std::array<std::byte, 4> bytes {std::byte{0x01}, std::byte{0x02}, std::byte{0x03}, std::byte{0x04}};
auto val = from_be_bytes<u32>(std::span<const std::byte, 4>{bytes});
// val == u32{0x01020304}
// Round-trip
auto original = u32{0xDEADBEEF};
auto reconstructed = from_be_bytes<u32>(std::span<const std::byte, 4>{to_be_bytes(original)});
// reconstructed == original