I would like an elegant, efficient means of taking any unsigned integer and converting it into the smallest byte array it will fit into. For example:
250 = byte[1]
2000 = byte[2]
80000 = byte[3]
so that I can write:
var foo = getBytes(bar);
and foo will be of different lengths depending on the value of bar. How would I do that?
You can do it like this, as an extension method:
Then:
Gives:
And replacing
j = 2000byj = 80000in the above givesAnd replacing
j = 2000byj = 250in the above gives