Your array isn't being shrunk, you have an entirely new array allocated inside BitConverter.GetBytes
.
Of course you can copy that output data into an array with size of your choice.
Or, just make your own version of BitConverter
. It's really simple:
byte[] tempPosBytes2 = new byte[] { (byte)(positiveNumber),
(byte)(positiveNumber >> 8),
(byte)(positiveNumber >> 16),
(byte)(positiveNumber >> 24),
0 };
I suggest you compare the performance using both techniques.
BTW, you could just use the BigInteger
constructor that takes a uint
.