Created
August 7, 2018 00:37
-
-
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.
This file contains hidden or 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
#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