# Convert 64 Byte QByteArray to two 32 Byte int

• I have a 64 byte long QByteArray. Something like this:

``````QByteArray sig = QByteArray::fromHex("3045022100f209c63ebba931be2b6bb6fffbe1d7e45a45e9ea48b835280d954ca7058953eb02204a145da934c1104a4d2253663bc0db4136b52cd62fe32cf383");
``````

Now I have to split it into two 32 Byte long QByteArray's

``````QByteArray r = sig.mid(0, 32);
QByteArray s = sig.mid(32, 32);
``````

Then I have to convert the QByteArray r and s into a big int.

And then I have to do some calculation on r and s and convert them back into a QByteArray.

How can I do this?

• Now I have to split it into two 32 Byte long QByteArray's

Then rather you intend: `QByteArray s = sig.mid(32, 32);`

Then I have to convert the QByteArray r and s into a big int.

Define "big int". You have 32 bytes (each) now. On a 64-bit machine `int`s go up to 8 bytes long. (Actually, it looks like mostly `int` will only be 32-bit, and even the largest language-supported `long long [int]` will, I think, only be 64-bits --- see e.g. https://en.cppreference.com/w/cpp/language/types.) So are you going to user some higher-precision numerical library? (E.g. I imagine boost might offer this.) " convert them back into a QByteArray" will depend on the package you use.

• How should this fit into a int64_t? A 64-bit (signed) integer can be max. 9223372036854775807 (hex: 0x7FFFFFFFFFFFFFFF) and your QByteArray is 32 Bytes long.

• @JonB It works with boost cpp_int. How can I convert a cpp_int back to a QByteArray?

• @Infinity If you can convert it to `const char []` or `std::string`, then you can also create a QByteArray from it.

Regards

• @aha_1980 I found a way

``````std::string str;
std::stringstream stream;
stream << std::hex << s;
str = stream.str();

QByteArray sByteArray = QByteArray::fromStdString(str);
``````

Is that correct or is there a cleaner way to do it?

• @Infinity I'm not sure there is a cleaner way, but it looks like that could work.

Regards

• Here is how I solved the problem. Just in case someone else faces a similar problem.

``````QByteArray sig = QByteArray::fromHex("3045022100f209c63ebba931be2b6bb6fffbe1d7e45a45e9ea48b835280d954ca7058953eb02204a145da934c1104a4d2253663bc0db4136b52cd62fe32cf383");

QByteArray rByteArray = sig.mid(0, 32);
QByteArray sByteArray = sig.mid(32, 32);

qDebug().noquote() << "r hex      :" << rByteArray.toHex();
qDebug().noquote() << "s hex      :" << sByteArray.toHex();

// Create a cpp_int
boost::multiprecision::cpp_int r("0x" + rByteArray.toHex().toStdString());

// Create a cpp_int
boost::multiprecision::cpp_int s("0x" + sByteArray.toHex().toStdString());

boost::multiprecision::cpp_int sum;

sum = r + s;

// Convert a cpp_int to a QByteArray
std::string str;
std::stringstream stream;
stream << std::hex << sum;
str = stream.str();

return QByteArray::fromStdString(str);
``````

• Why to conversion to QString and std::string at all? cpp_int takes a const char * afaics.

• @Christian-Ehrlicher I need to insert "0x" at the beginning. Could I also insert it directly in the QByteArray?

• Could I also insert it directly in the QByteArray?

What's wrong with the documentation?

• @Christian-Ehrlicher Thank you very much. I changed the example above accordingly. How does that look like?