As you may know, 0xFFFFFFFF in the Two's Complement representation equals -1 (for 32 bits). But the following code:

qint32 aa = QString("FFFFFFFF").toInt(0, 16);
qDebug()<<aa;

prints 0. The code below:

qint32 aa = 0xffffffff;
qDebug()<<aa;

prints -1!

Why is this?

有帮助吗?

解决方案

If you read the documentation you can see that toInt "Returns 0 if the conversion fails."

Your input does not fit in a signed 32 bit integer, so presumably the conversion fails.

You can verify this by using the ok-parameter:

bool ok;
qint32 aa = QString("FFFFFFFF").toInt(&ok, 16);
if (ok) qDebug() << aa;
else qDebug() << "Conversion failed!";

其他提示

FFFFFFFF is not an integer format. 0xFFFFFFFF is. And also 0xFFFFFFFF is out of int range. Try use usigned integer. Change the conversion function toInt to toUInit. Here is the code:

quint32 aa = QString("0xffffffff").toUInt(0, 16);
quint32 bb = 0xffffffff;
qDebug()<<aa;
qDebug()<<bb;

It has been tested on my machine. The output is 4294967295.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top