Пример #1
0
 public static long WriteCompactToBuf(CompactIntegerLEBytes ci, ref byte[] buf, long offset)
 {
     for (int i = 0; i < ci.Length; ++i)
     {
         buf[offset + i] = ci.Bytes[i];
     }
     return(ci.Length);
 }
Пример #2
0
        public static CompactIntegerLEBytes EncodeCompactInteger(BigInteger n)
        {
            CompactIntegerLEBytes b = new CompactIntegerLEBytes();

            b.Bytes = new byte[64];

            if (n <= 63)
            {
                b.Length   = 1;
                b.Bytes[0] = (byte)(n << 2);
            }
            else if (n <= 0x3FFF)
            {
                b.Length   = 2;
                b.Bytes[0] = (byte)(((n & 0x3F) << 2) | 0x01);
                b.Bytes[1] = (byte)((n & 0xFC0) >> 6);
            }
            else if (n <= 0x3FFFFFFF)
            {
                b.Length   = 4;
                b.Bytes[0] = (byte)(((n & 0x3F) << 2) | 0x02);
                n        >>= 6;
                for (int i = 1; i < 4; ++i)
                {
                    b.Bytes[i] = (byte)(n & 0xFF);
                    n        >>= 8;
                }
            }
            else
            { // Big integer mode
                b.Length = 1;
                int byteNum = 1;
                while (n > 0)
                {
                    b.Bytes[byteNum++] = (byte)(n & 0xFF);
                    n >>= 8;
                }
                b.Length   = byteNum;
                b.Bytes[0] = (byte)(((byteNum - 5) << 2) | 0x03);
            }

            b.Bytes = b.Bytes.AsMemory().Slice(0, (int)b.Length).ToArray();

            return(b);
        }