SnmpSharpNet.AsnType.BuildLength C# (CSharp) Method

BuildLength() static private method

Append BER encoded length to the MutableByte
Thrown when length value to encode is less then 0
static private BuildLength ( MutableByte mb, int asnLength ) : void
mb MutableByte MutableArray to append BER encoded length to
asnLength int Length value to encode.
return void
        internal static void BuildLength(MutableByte mb, int asnLength)
        {
            if (asnLength < 0)
                throw new ArgumentOutOfRangeException("Length cannot be less then 0.");
            byte[] len = BitConverter.GetBytes(asnLength);
            MutableByte buf = new MutableByte();
            for (int i = 3; i >= 0; i--)
            {
                if (len[i] != 0 || buf.Length > 0)
                    buf.Append(len[i]);
            }
            if (buf.Length == 0)
            {
                // we are encoding a 0 value. Can't have a 0 byte length encoding
                buf.Append(0);
            }
            // check for short form encoding
            if (buf.Length == 1 && (buf[0] & HIGH_BIT) == 0)
                mb.Append(buf); // done
            else
            {
                // long form encoding
                byte encHeader = (byte)buf.Length;
                encHeader = (byte)(encHeader | HIGH_BIT);
                mb.Append(encHeader);
                mb.Append(buf);
            }
        }