src/NeinMath/IntegerConverter.cs
/// <summary>
/// Returns the specified Integer as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes.</returns>
Returns the specified Integer as an array of bytes.
Returns the specified Integer as an array of bytes .
public static byte[] ToByteArray(this Integer value)
        {
            if (value.length == 0)
                return new byte[] { 0 };
            var bits = value.length * 32
                - Bits.LeadingZeros(value.bits[value.length - 1]) + 1;
            var bytes = new byte[(bits + 7) / 8];
            for (var i = 0; i < (bits + 6) / 8; i++)
            {
                var shift = (i % 4) * 8;
                bytes[i] = (byte)(value.bits[i / 4] >> shift);
            }
            if (value.sign)
                bytes = Bits.TwosComplement(bytes, bytes.Length);
            return bytes;
        }
public static byte [ ] ToByteArray ( this Integer value ) { if ( value . length == 0 ) return new byte [ ] { 0 } ; var bits = value . length * 32 - Bits . LeadingZeros ( value . bits [ value . length - 1 ] ) + 1 ; var bytes = new byte [ ( bits + 7 ) / 8 ] ; for ( var i = 0 ; i < ( bits + 6 ) / 8 ; i ++ ) { var shift = ( i % 4 ) * 8 ; bytes [ i ] = ( byte ) ( value . bits [ i / 4 ] >> shift ) ; } if ( value . sign ) bytes = Bits . TwosComplement ( bytes , bytes . Length ) ; return bytes ; }
Returns the specified Integer as an array of bytes.
Returns the specified Integer as an array of bytes .
An array of bytes.
An array of bytes .
Integer
this
value
The number to convert.
The number to convert .