Well the simplest general purpose solution here would be
a = b1 * 256 + b2;
aka
a = (b1 << 8) | b2;
Then to get them back (assuming you've got unsigned bytes available):
b1 = (a >> 8) & 0xff;
b2 = a & 0xff;
That will produce a 2-byte value for all inputs, unless you deem the results of b1=0, b2=*
to be one byte (as every value is less than 256). You could potentially interleave things, such that the low four bits of each input byte ends up in the low eight bits of the output, so that you'd end up with a value of less than 256 for (b1 < 16, b2 < 16
). Frankly it's easiest to always just consider it as a 2-byte value, using the above shifting.
Using 2 bytes is clearly a minimum, and it's easily achievable.
If that's not what you're looking for, please give more information.
EDIT: I've been assuming you're looking for a fixed binary length. If you want a fixed decimal length, use falagar's solution.