Secp256k1.Net.Test/Tests.cs
/// <summary>
/// Obtains the bytes that represent the BigInteger as if it was a big endian 256-bit integer.
/// </summary>
/// <param name="bigInteger">The BigInteger to obtain the byte representation of.</param>
/// <returns>Returns the bytes that represent BigInteger as if it was a 256-bit integer.</returns>
Obtains the bytes that represent the BigInteger as if it was a big endian 256-bit integer.
Obtains the bytes that represent the BigInteger as if it was a big endian 256 - bit integer .
public static byte[] GetBytes(BigInteger bigInteger, int byteCount = 32)
        {
            byte[] result = bigInteger.ToByteArray();
            if (!BitConverter.IsLittleEndian)
                Array.Reverse(result);
            int originalSize = result.Length;
            Array.Resize(ref result, byteCount);
            if (bigInteger.Sign < 0)
                for (int i = originalSize; i < result.Length; i++)
                    result[i] = 0xFF;
            Array.Reverse(result);
            return result;
        }
public static byte [ ] GetBytes ( BigInteger bigInteger , int byteCount = 32 ) { byte [ ] result = bigInteger . ToByteArray ( ) ; if ( ! BitConverter . IsLittleEndian ) Array . Reverse ( result ) ; int originalSize = result . Length ; Array . Resize ( ref result , byteCount ) ; if ( bigInteger . Sign < 0 ) for ( int i = originalSize ; i < result . Length ; i ++ ) result [ i ] = 0xFF ; Array . Reverse ( result ) ; return result ; }
Obtains the bytes that represent the BigInteger as if it was a big endian 256-bit integer.
Obtains the bytes that represent the BigInteger as if it was a big endian 256 - bit integer .
// Obtain the bytes which represent this BigInteger. // We'll operate on the data in little endian (since we'll extend the array anyways and we'd have to copy the data over anyways). // Store the original size of the data, then resize it to the size of a word. // BigInteger uses the most significant bit as sign and optimizes to return values like -1 as 0xFF instead of as 0xFFFF or larger (since there is no bound size, and negative values have all leading bits set) // Instead if we wanted to represent 256 (0xFF), we would add a leading zero byte so the sign bit comes from it, and will be zero (positive) (0x00FF), this way, BigInteger knows to represent this as a positive value. // Because we resized the array already, it would have added leading zero bytes which works for positive numbers, but if it's negative, all extended bits should be set, so we check for that case. // If the integer is negative, any extended bits should all be set. // Flip the array so it is in big endian form.
Returns the bytes that represent BigInteger as if it was a 256-bit integer.
Returns the bytes that represent BigInteger as if it was a 256 - bit integer .
bigInteger
BigInteger
The BigInteger to obtain the byte representation of.
The BigInteger to obtain the byte representation of .
byteCount
int