Converting unsigned integer value into byte array I have a function that takes an unsigned value and transforms it into a std::vector&lt;std::uint8_t&gt;. The requirements (and post conditions) are as follows:


The code must utilize "modern C++" up to C++14
The focus is on readability/maintainability first, performance second
The most significant byte must be at index 0.
The lease significant byte must be at index size()-1.
The return type cannot be changed; this is a requirement stemming from other code not shown in this context (larger application requirement). Also avoids a conversion from one container to std::vector.
The resulting vector must be as compact as possible. For values that decompose into fewer bytes than sizeof(unsigned), the resulting vector must not contain unnecessary MSB's (that is, zero value bytes).
The code must be endianness-agnostic and portable to various other architectures and platforms.


Here is the current implementation I have:

using BinaryStreamByteVector = std::vector&lt;std::uint8_t&gt;;

BinaryStreamByteVector CreateMultiByteInteger(unsigned value)
{
    BinaryStreamByteVector value_bytes;

    while (value != 0)
    {
        std::uint8_t byte = value &amp; 0xFF;
        value_bytes.insert(value_bytes.begin(), byte);

        value &gt;&gt;= 8;
    }

    return value_bytes;
}


(Live Sample on Coliru)

I can't think of a better implementation for CreateMultiByteInteger, so I'm hoping for other ideas that meet the requirements above.
Observations

Your code is beautiful (proper naming conventions followed, proper whitespace usage, very readable. I particularly like the type-aliasing, which helps make the whole thing that much more readable than a typedef or #define). I cannot personally think of any more ways to make your code more readable, however, I do have 1 suggestion.

The algorithm you currently follow is standard and ideal (you even have proper masking of higher-order bits); and in my honest opinion any other algorithm would be tantamount to obfuscation; but if you assume that the code will only ever run on a particular architecture, you could probably unroll the loop.

Suggestion


Make your function parameter const. Although C++ is call-by-value, making it explicitly const and mutating a local copy of the variable is never a bad idea, and it does make your function contract that much clearer. 
On the performance side, since on any reasonable computer unsigned will have maximally 16 bytes (64 bits of 8 bytes is the case for all common architectures, except for probably some microcontrollers which will have less, even as less as 1 byte) (note that the upcoming RISCV architecture does have a native 16-byte (128-bit) integral data type, but the 64-bit one is the default), you can easily make this function a template, such that the computation of the byte vector can be done at compile time without much overhead and incur no runtime overhead. 

You can probably even pre-set the capacity of your std::vector utilising sizeof(unsigned).

Although I'm not sure if this suggestion will work in the constraints of your application.