Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString.toInt() does not convert special hex numbers
Forum Updated to NodeBB v4.3 + New Features

QString.toInt() does not convert special hex numbers

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbreier
    wrote on last edited by
    #1

    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?

    Regards
    Michael

    D 1 Reply Last reply
    0
    • M mbreier

      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?

      Regards
      Michael

      D Offline
      D Offline
      Dani
      wrote on last edited by Dani
      #2

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

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zerocom
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved