I am using GMP in a QT project, and I am trying to use QByteArray to store some of the numbers.
When I want to convert a byte array to an mpz_t I am doing the following:
const char *hexstring = qbyteval.toHex().toStdString().c_str();
mpz_t myval;
mpz_init (myval);
mpz_set_str(myval, hexstring, 16);
mpz_t zero;
mpz_init(zero);
mpz_set_str(zero, "0", 10);
if(mpz_comp(myval, zero) == 0) {
//...set a breakpoint or do something here. This line shouldn't be reached ever.
}
In my case, qbyteval
is never zero. If I set a breakpoint inside that mpz_comp if block, qbyteval.toHex() is always still a hex string.
What is failing, that is causing hexstring to be an empty string (and thus myval =0)?
This looks wrong:
const char *hexstring = qbyteval.toHex().toStdString().c_str();
The c_str()
function returns a pointer to the internal data in a std::string
, and that pointer can become invalidated if you modify or destroy the std::string
. In this case, the std::string
gets destroyed right after that line runs, because you didn't store it in a variable.
There might be more errors like this in your code, but that is the most glaring error. If you still have trouble, I suggest posting a minimal, complete example and better explaining the expected behavior vs the actual behavior, and using a debugger to look at what is happening on every line.