Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-119407

QString::toShort unable to convert hex string greater than "7FFF"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Not Evaluated Not Evaluated
    • None
    • 5.15.2
    • None
    • MSVC2019
    • Windows

      While using QString::toShort to convert a 4-digit string in base 16, it always returns 0 if the string is greater than "7FFF".

      For example: QString("FF73").toShort(nullptr, 16) will return 0.

      The qt source code is as follows:

      short QString::toShort(bool *ok, int base) const
      {
          long v = toLongLong(ok, base);
          if (v < SHRT_MIN || v > SHRT_MAX)

      Unknown macro: {         if (ok)                           *ok = false;                 v = 0;         }

          return (short)v;
      }

      where SHRT_MIN  is -32768 and SHRT_MAX is 32767.

      Take "FF73" as example, converting "FF73" to a longlong will be 65395. Then this function will consider it out of range and return 0. However, shouldn't this function be able to convert string "0000" to "FFFF" to signed short -32768 to 32767? Now it can only convert string "0000" to "7FFF", and "8000" to "FFFF" will be considered as out of range and always return 0.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            thiago Thiago Macieira
            djkjq Jian Dong
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes