I am implementing an LZW compression/decompression utility library and am in need of returning the compressed output in what I am using as:
using ByteSequence = std::vector<std::uint8_t>
The output format for the compressor will include the positions in the compressor's dictionary of various sequences found by the algorithm. For example, having 16-bit positions in the output would look like:
std::vector<std::uint16_t> pos{123, 385, /* ... */};
The output, however needs to be a ByteSequence
, and it needs to be portable among architectures. What I am currently doing to convert the pos
vector to the desired format is:
for (auto p : pos)
{
std::uint8_t *bytes = (std::uint8_t *) &p;
output.push_back(bytes[0]);
output.push_back(bytes[1]);
}
This works, but only under the assumption that the keys will be 16-bit each and to be honest, it looks like a cheap trick to me.
How should I do this in a better, cleaner way? Thank you!