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.