If you want to store an integer in a byte array, the best approach is probably to just cast the integer to a uint8_t*
and copy it into an std::array
. You're going to have to use raw pointers at some point, so your best option is to encapsulate the operation into a function.
template<typename T>
std::array<uint8_t, sizeof(T)> ScalarToByteArray(const T value)
{
static_assert(std::is_integral<T>::value,
"Template parameter must be a scalar type");
std::array<uint8_t, sizeof(T)> result;
std::copy((uint8_t*)&value, ((uint8_t*)&value) + sizeof(T), result.begin());
return result;
}