BitSharp.Core.Rules.CoreRules.SerializeScriptValue C# (CSharp) Method

SerializeScriptValue() private method

private SerializeScriptValue ( long value ) : byte[]
value long
return byte[]
        private byte[] SerializeScriptValue(long value)
        {
            if (value == 0)
                return new byte[0];

            var result = new List<byte>();

            var neg = value < 0;
            var absvalue = (ulong)(neg ? -value : value);

            while (absvalue != 0)
            {
                result.Add((byte)(absvalue & 0xff));
                absvalue >>= 8;
            }

            //    - If the most significant byte is >= 0x80 and the value is positive, push a
            //    new zero-byte to make the significant byte < 0x80 again.

            //    - If the most significant byte is >= 0x80 and the value is negative, push a
            //    new 0x80 byte that will be popped off when converting to an integral.

            //    - If the most significant byte is < 0x80 and the value is negative, add
            //    0x80 to it, since it will be subtracted and interpreted as a negative when
            //    converting to an integral.

            if ((result.Last() & 0x80) != 0)
                result.Add((byte)(neg ? 0x80 : 0));
            else if (neg)
                result[result.Count - 1] = (byte)(result.Last() | 0x80);

            return result.ToArray();
        }