How do we convert mpz_t to std::string?
mpz_t Var;
// Var = 5000
mpz_init_set_ui( Var, 5000 );
std::string Str = "";
// Convert Var to std::string?
mpz_clear( Var );
You're looking for mpz_get_str
:
char * tmp = mpz_get_str(NULL,10,Var);
std::string Str = tmp;
// In order to free the memory we need to get the right free function:
void (*freefunc)(void *, size_t);
mp_get_memory_functions (NULL, NULL, &freefunc);
// In order to use free one needs to give both the pointer and the block
// size. For tmp this is strlen(tmp) + 1, see [1].
freefunc(tmp, strlen(tmp) + 1);
However, you shouldn't use mpz_t
in a C++ program. Use mpz_class
instead, since it provides the get_str()
method, which actually returns a std::string
and not a pointer to some allocated memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With