BitConverter.GetBytes always returns fixed length arrays for numbers, padded with zeros so that they are precisely 1, 2, 4, or 8 bytes long. Similarly, the decoding methods only accept arrays of certain lengths, depending on the size of the number.
I’d prefer that GetBytes returned unpadded arrays and that the decoding methods accepted unpadded arrays, so that I’m not always having to do the padding and trimming myself. For example:
- I’d like
GetBytes(100000)to return{ 0x01, 0x86, 0xa0 }instead of{ 0x00, 0x01, 0x86, 0xa0 } - I’d like to pass
{ 0x01, 0x86, 0xa0 }toToInt32instead of{ 0x00, 0x01, 0x86, 0xa0 }
Am I missing some static property or different method of BitConverter—or perhaps some other class or third-party library—that gives me what I want without having to do the padding and trimming myself?
You can write your own implementation of
GetBytes()andToInt32()for that.similarly
ToInt32():