How to initialize a mpz_t in gmp with a 1024-bit number from a character string?

c gmp
XyZ picture XyZ · Jul 13, 2011 · Viewed 9.4k times · Source

I want to initialize a mpz_t variable in gmp with a very large value like a 1024 bit large integer. How can I do so ? I am new to gmp. Any help would be appreciated.

Answer

Adam Rosenfield picture Adam Rosenfield · Jul 13, 2011

Use mpz_import. For example:

uint8_t input[128];
mpz_t z;
mpz_init(z);

// Convert the 1024-bit number 'input' into an mpz_t, with the most significant byte
// first and using native endianness within each byte.
mpz_import(z, sizeof(input), 1, sizeof(input[0]), 0, 0, input);