コード例 #1
0
        internal static Array BigIntegerToBytes(BigInteger b, int numBytes)
        {
            if (b == null)
            {
                return(null);
            }
            byte[] bytes   = new byte[numBytes];
            byte[] biBytes = b.ToByteArray();
            int    start   = (biBytes.Length == numBytes + 1) ? 1 : 0;
            int    length  = Math.Min(biBytes.Length, numBytes);

            Array.Copy(biBytes, start, bytes, numBytes - length, length);
            return(bytes);
        }
コード例 #2
0
        internal static byte[] BigIntegerToBytes(BigInteger num)
#endif
        {
            if (num == 0)
            {
                //Positive 0 is represented by a null-length vector
                return(new byte[0]);
            }

            bool isPositive = true;

            if (num < 0)
            {
                isPositive = false;
                num       *= -1;
            }
            var array = num.ToByteArray();

            if (!isPositive)
            {
                array[array.Length - 1] |= 0x80;
            }
            return(array);
        }