Click or drag to resize
gmp_libmpz_export Method (void_ptr, size_t, Int32, size_t, Int32, size_t, mpz_t)
Fill rop with word data from op.

Namespace:  Math.Gmp.Native
Assembly:  Math.Gmp.Native (in Math.Gmp.Native.dll) Version: (
public static void_ptr mpz_export(
	void_ptr rop,
	ref size_t countp,
	int order,
	size_t size,
	int endian,
	size_t nails,
	mpz_t op


Type: Math.Gmp.Nativevoid_ptr
The result integer.
Type: Math.Gmp.Nativesize_t
The number of words produced.
Type: SystemInt32
1 for most significant word first or -1 for least significant first.
Type: Math.Gmp.Nativesize_t
The number of bytes in each word.
Type: SystemInt32
1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU.
Type: Math.Gmp.Nativesize_t
The number of most significant bits to skip.
Type: Math.Gmp.Nativempz_t
The operand integer.

Return Value

Type: void_ptr
Either rop or the allocated block.

The parameters specify the format of the data produced. Each word will be size bytes and order can be 1 for most significant word first or -1 for least significant first. Within each word endian can be 1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU. The most significant nails bits of each word are unused and set to zero, this can be 0 to produce full words.

The number of words produced is written to countp, or countp can be NULL to discard the count. rop must have enough space for the data, or if rop is NULL then a result array of the necessary size is allocated using the current GMP allocation function (see GNU MP - Custom Allocation). In either case the return value is the destination used, either rop or the allocated block.

If op is non-zero then the most significant word produced will be non-zero. If op is zero then the count returned will be zero and nothing written to rop. If rop is NULL in this case, no block is allocated, just NULL is returned.

The sign of op is ignored, just the absolute value is exported. An application can use mpz_sgn to get the sign and handle it as desired. (see GNU MP - Integer Comparisons)

There are no data alignment restrictions on rop, any address is allowed.

When an application is allocating space itself the required size can be determined with a calculation like the following. Since mpz_sizeinbase always returns at least 1, count here will be at least one, which avoids any portability problems with malloc(0), though if z is zero no space at all is actually needed (or written).

numb = 8 * size - nail;
count = (mpz_sizeinbase(z, 2) + numb - 1) / numb;
p = malloc(count * size);
// Create, initialize, and set the value of op to 0x800000000000000000000001.
mpz_t op = new mpz_t();
char_ptr value = new char_ptr("800000000000000000000001");
gmp_lib.mpz_init_set_str(op, value, 16);

// Export op as 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb.
void_ptr data = gmp_lib.allocate(12);
size_t countp = 0;
gmp_lib.mpz_export(data, ref countp, -1, 4, 1, 0, op);

// Assert the result.
byte[] result = new byte[12];
Marshal.Copy(data.ToIntPtr(), result, 0, 12);
Assert.IsTrue(result[0] == 0x00);
Assert.IsTrue(result[1] == 0x00);
Assert.IsTrue(result[2] == 0x00);
Assert.IsTrue(result[3] == 0x01);
Assert.IsTrue(result[4] == 0x00);
Assert.IsTrue(result[5] == 0x00);
Assert.IsTrue(result[6] == 0x00);
Assert.IsTrue(result[7] == 0x00);
Assert.IsTrue(result[8] == 0x80);
Assert.IsTrue(result[9] == 0x00);
Assert.IsTrue(result[10] == 0x00);
Assert.IsTrue(result[11] == 0x00);

// Release unmanaged memory allocated for rop, data, and value.
See Also