Skip to content

Instantly share code, notes, and snippets.

@jdiego
Created August 7, 2018 00:37
Show Gist options
  • Save jdiego/350707746286fd55cb8f9c1ff641b8a0 to your computer and use it in GitHub Desktop.
Save jdiego/350707746286fd55cb8f9c1ff641b8a0 to your computer and use it in GitHub Desktop.
Convert size_t (4 or 8) bytes to array of bytes same length.
#include <array>
#include <cstddef>
constexpr auto dissolve(size_t value) noexcept {
return [=]<size_t... I>(std::index_sequence<I...>) noexcept -> std::array<uint8_t, sizeof(size_t)>{
return { static_cast<uint8_t>((value >> (((sizeof(size_t)-1)*8) - (8 * I)) ) & 0xFF)... };
}(std::make_index_sequence<sizeof(size_t)>());
}
auto convert2(size_t v) {
return dissolve(v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment