Problems with Open-Source Downloads read and

QString.toInt() does not convert special hex numbers

  • Hello,

    I tried to convert 2 hex numbers stored in a QString into a number using the toInt() method.
    The first number is 0x5AC366BD which converts fine.
    The second number is 0xA53C3CDB which failed to convert.
    The code is the following:

    QString Signature1 = "0x5AC366BD";
    QString Signature2 = "0xA53C3CDB";
    bool ok;
    qint32 sig1 = Signature1.toInt(&ok,0);
    qint32 sig2 = Signature2.toInt(&ok,0);

    So nothing special I thought.

    I use Qt 5.4.1 compiled with GCC 4.9.2 64 Bit and QtCreator 3.3.2

    Does anybody have an Idea what may be the problem?


  • @mbreier the second number is greater than the maximum value of qint32.

  • Try toUInt(&ok,0) and convert it later to qint32.

  • Moderators

    @zerocom This is implementation defined and will return a negative qint32 on some implementations and truncated positive value on others.
    The correct thing to do is to use toLongLong. You can then inspect the value as a long long and decide what to do with it next, depending on what you expect (a negative or positive number), and provide a correct manual conversion or truncation.

Log in to reply