I'm working with ASN.1 encoding and want to convert byte arrays of different lengths to integers without writing a separate case for each length. BitConverter.ToInt32 expects four bytes, and converting an integer back produces four bytes even for a small value.
For example, I want [byte[]]@(127, 0, 0) to give 127. Is there a way to handle this as an unsigned value, and control the byte order?
Sola:
The example is little-endian, but ASN.1 INTEGER content uses big-endian signed two's complement. Use the signed setting when decoding an ASN.1 INTEGER rather than assuming every value is unsigned.