Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString::toShort problem
Qt 6.11 is out! See what's new in the release blog

QString::toShort problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
58 Posts 7 Posters 73.6k Views 3 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.
  • jsulmJ jsulm

    @JonB said in QString::toShort problem:

    Since QString::toLongLong() returns a qint64 (8 bytes), did you try 0xFFFFFFFFFFFFFFFE ?

    Returns 0 as well.
    And I don't see why it should depend on the length.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #28

    @jsulm
    It would "depend on the length", as you put it, because as a 64-bit number 0xFFFFFFFE != 0xFFFFFFFFFFFFFFFE.

    jsulmJ 2 Replies Last reply
    0
    • JonBJ JonB

      @jsulm
      It would "depend on the length", as you put it, because as a 64-bit number 0xFFFFFFFE != 0xFFFFFFFFFFFFFFFE.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #29

      @JonB I want to convert a signed short number not long or long long or ...
      0xFFFE as signed short is -2 - do you agree (I mean independently from what Qt toShort() thinks it is)?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      0
      • JonBJ JonB

        @jsulm
        It would "depend on the length", as you put it, because as a 64-bit number 0xFFFFFFFE != 0xFFFFFFFFFFFFFFFE.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #30

        @JonB

        qDebug() << (short)0xFFFE;
        

        prints -2 as expected

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J.HilkJ JonBJ 2 Replies Last reply
        0
        • jsulmJ jsulm

          @JonB I want to convert a signed short number not long or long long or ...
          0xFFFE as signed short is -2 - do you agree (I mean independently from what Qt toShort() thinks it is)?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #31

          @jsulm
          I believe the problem here is a confusion between "bit representation" and "string representation".

          • It is undoubtedly, unambiguously true that, for signed short, 0xFFFE as a bit pattern is -2.
          • However, for signed short, 0xFFFE as a string "could" be either -2 (which fits in a short) or 65,534 (which does not fit in a short). And QString::toShort() is taking the latter interpretation, and hence erroring.
          jsulmJ 1 Reply Last reply
          1
          • jsulmJ jsulm

            @JonB

            qDebug() << (short)0xFFFE;
            

            prints -2 as expected

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #32

            @jsulm said in QString::toShort problem:

            @JonB

            qDebug() << (short)0xFFFE;
            

            prints -2 as expected

            qDebug() << (short)0xFFFFFFFFFFFFFFFE;

            prints also -2, would one expect that
            0_1530172437505_306d84bf-e9ce-4724-acc5-5efc6bd718b5-image.png

            actually yes, the first bytes are simply dropped x)


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @JonB

              qDebug() << (short)0xFFFE;
              

              prints -2 as expected

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #33

              @jsulm said in QString::toShort problem:

              @JonB

              qDebug() << (short)0xFFFE;
              

              prints -2 as expected

              Yes, that's why I wrote earlier:

              One thing that is clear: the implementation of QString::toShort() is not static_cast<short>(QString::toUShort()), even if that might have been the way you were tempted to do it.

              jsulmJ 1 Reply Last reply
              0
              • JonBJ JonB

                @jsulm
                I believe the problem here is a confusion between "bit representation" and "string representation".

                • It is undoubtedly, unambiguously true that, for signed short, 0xFFFE as a bit pattern is -2.
                • However, for signed short, 0xFFFE as a string "could" be either -2 (which fits in a short) or 65,534 (which does not fit in a short). And QString::toShort() is taking the latter interpretation, and hence erroring.
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #34

                @JonB said in QString::toShort problem:

                However, for signed short, 0xFFFE as a string "could" be either -2 (which fits in a short) or 65,534

                No, signed short 0xFFFE is -2 even as string, because I'm calling toShort() not toUShort().
                And why does

                qDebug() << (short)0xFFFE;
                

                print -2?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • JonBJ JonB

                  @jsulm said in QString::toShort problem:

                  @JonB

                  qDebug() << (short)0xFFFE;
                  

                  prints -2 as expected

                  Yes, that's why I wrote earlier:

                  One thing that is clear: the implementation of QString::toShort() is not static_cast<short>(QString::toUShort()), even if that might have been the way you were tempted to do it.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #35

                  @JonB said in QString::toShort problem:

                  the implementation of QString::toShort() is not static_cast<short>(QString::toUShort())

                  I never said that

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonBJ 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @JonB said in QString::toShort problem:

                    the implementation of QString::toShort() is not static_cast<short>(QString::toUShort())

                    I never said that

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #36

                    @jsulm
                    But you're asking why qDebug() << (short)0xFFFE; prints -2. And I'm saying that's because of the way "cast-to-short" works in C++, which is simply not what the implementation of Qt's QString::toShort() does or purports to do.

                    Basically, "cast-to-short" ((short)) has no concept ever of "overflow/error", but QString::toShort() does have a concept of "overflow/error", and that's why they work differently. They are not intended to be equivalent.

                    [I am beginning to feel the need for @kshegunov 's moral support here, because I feel I am being attacked ( :( ) and it is indeed all to do with the overflowing he mentioned in his earlier reply.]

                    jsulmJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @jsulm
                      But you're asking why qDebug() << (short)0xFFFE; prints -2. And I'm saying that's because of the way "cast-to-short" works in C++, which is simply not what the implementation of Qt's QString::toShort() does or purports to do.

                      Basically, "cast-to-short" ((short)) has no concept ever of "overflow/error", but QString::toShort() does have a concept of "overflow/error", and that's why they work differently. They are not intended to be equivalent.

                      [I am beginning to feel the need for @kshegunov 's moral support here, because I feel I am being attacked ( :( ) and it is indeed all to do with the overflowing he mentioned in his earlier reply.]

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #37

                      @JonB What overflow error do you mean? 0xFFFE is a valid short number in both cases: signed and unsigned.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      JonBJ 1 Reply Last reply
                      0
                      • kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #38

                        Hey, let's do it the russian way and settle this outside, huh? Take a breath people.

                        @jsulm
                        Johann, you're wrong simply because "0xFFFE" is not a negative number, but a string, that simple. I know that in 2's complement for short this is -2, but that's if you go to the actual implementation of the negative numbers. The fact of the matter is there have been implementations that do not use integer complements. This string is not a binary representation, that is all, so don't expect the function to assume it should convert in binary-like way! Otherwise, as Jonas pointed out earlier "0xFFFFFFFFFFE" should just expand to -2 as well due to truncations.

                        Read and abide by the Qt Code of Conduct

                        JonBJ jsulmJ 2 Replies Last reply
                        2
                        • jsulmJ jsulm

                          @JonB What overflow error do you mean? 0xFFFE is a valid short number in both cases: signed and unsigned.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #39

                          @jsulm

                          @JonB What overflow error do you mean? 0xFFFE is a valid short number in both cases: signed and unsigned.

                          0xFFFE as a bit-pattern is indeed a valid signed or unsigned bit-pattern for a short. But as a string to parse, for QString::toUShort() it's valid (65,534, which is OK for ushort), but for QString::toShort() it's a positive number greater than the positive limit of 32,767 for a short ("overflow").

                          1 Reply Last reply
                          0
                          • kshegunovK kshegunov

                            Hey, let's do it the russian way and settle this outside, huh? Take a breath people.

                            @jsulm
                            Johann, you're wrong simply because "0xFFFE" is not a negative number, but a string, that simple. I know that in 2's complement for short this is -2, but that's if you go to the actual implementation of the negative numbers. The fact of the matter is there have been implementations that do not use integer complements. This string is not a binary representation, that is all, so don't expect the function to assume it should convert in binary-like way! Otherwise, as Jonas pointed out earlier "0xFFFFFFFFFFE" should just expand to -2 as well due to truncations.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #40

                            @kshegunov said in QString::toShort problem:

                            Hey, let's do it the russian way and settle this outside, huh? Take a breath people.

                            LOL! Phew, that's what I needed from you! I thought you might be Russian: are you "Mafiosa", could you send some "heavies" round to @jsulm for me...? ;-)

                            1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              Hey, let's do it the russian way and settle this outside, huh? Take a breath people.

                              @jsulm
                              Johann, you're wrong simply because "0xFFFE" is not a negative number, but a string, that simple. I know that in 2's complement for short this is -2, but that's if you go to the actual implementation of the negative numbers. The fact of the matter is there have been implementations that do not use integer complements. This string is not a binary representation, that is all, so don't expect the function to assume it should convert in binary-like way! Otherwise, as Jonas pointed out earlier "0xFFFFFFFFFFE" should just expand to -2 as well due to truncations.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #41

                              @kshegunov "Hey, let's do it the russian way and settle this outside, huh?" - wait a bit I need to collect some more guys to have better arguments :-)
                              OK, I see. But actually Qt "knows" for which platform it was built (2's complement or something else) and could interpret such strings accordingly. I guess Qt devs wanted to go safe route :-)
                              @Bart_Vandewoestyne I would say @kshegunov suggested the correct solution:

                              short hex2 = static_cast<short>(str2.toUShort(&ok2, 16));
                              

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • J.HilkJ J.Hilk

                                @JonB surprisingly enough

                                qDebug() << std::numeric_limits<int64_t>::min() << std::numeric_limits<int64_t>::max()
                                             << endl << (int64_t)0xFFFFFFFFFFFFFFFE;
                                 QString s("0xFFFFFFFFFFFFFFFE"); bool ok;
                                short sh =  s.toShort(&ok, 16);
                                qDebug() <<sh << ok;
                                long lg = s.toLongLong(&ok,16);
                                qDebug() << lg << ok;
                                

                                returns:

                                -9223372036854775808 9223372036854775807 
                                -2
                                0 false
                                0 false
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #42

                                @J.Hilk said in QString::toShort problem:

                                QString s("0xFFFFFFFFFFFFFFFE"); bool ok;
                                long lg = s.toLongLong(&ok,16);

                                What is it in the implementation of qint64 QString::toLongLong() which makes this set ok=false instead of returning -2 ?

                                J.HilkJ kshegunovK 2 Replies Last reply
                                0
                                • JonBJ JonB

                                  @J.Hilk said in QString::toShort problem:

                                  QString s("0xFFFFFFFFFFFFFFFE"); bool ok;
                                  long lg = s.toLongLong(&ok,16);

                                  What is it in the implementation of qint64 QString::toLongLong() which makes this set ok=false instead of returning -2 ?

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #43

                                  @JonB
                                  thatsa a rapidhole down QString and QLocal ... 😨😨

                                  Still to early in the morning to explore that ;-)


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @J.Hilk said in QString::toShort problem:

                                    QString s("0xFFFFFFFFFFFFFFFE"); bool ok;
                                    long lg = s.toLongLong(&ok,16);

                                    What is it in the implementation of qint64 QString::toLongLong() which makes this set ok=false instead of returning -2 ?

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by
                                    #44

                                    @JonB said in QString::toShort problem:

                                    I thought you might be Russian: are you "Mafiosa", could you send some "heavies" round to @jsulm for me...?

                                    No, I'm not russian, but let's assume I know a guy who knows a guy, who knows a guy ... ;)

                                    What is it in the implementation of qint64 QString::toLongLong() which makes this set ok=false instead of returning -2 ?

                                    https://code.woboq.org/qt5/qtbase/src/corelib/tools/qlocale.cpp.html#_ZNK11QLocaleData15numberToCLocaleE11QStringView6QFlagsIN7QLocale12NumberOptionEEP15QVarLengthArrayIcLi256EE

                                    Read and abide by the Qt Code of Conduct

                                    JonBJ 1 Reply Last reply
                                    0
                                    • kshegunovK kshegunov

                                      @JonB said in QString::toShort problem:

                                      I thought you might be Russian: are you "Mafiosa", could you send some "heavies" round to @jsulm for me...?

                                      No, I'm not russian, but let's assume I know a guy who knows a guy, who knows a guy ... ;)

                                      What is it in the implementation of qint64 QString::toLongLong() which makes this set ok=false instead of returning -2 ?

                                      https://code.woboq.org/qt5/qtbase/src/corelib/tools/qlocale.cpp.html#_ZNK11QLocaleData15numberToCLocaleE11QStringView6QFlagsIN7QLocale12NumberOptionEEP15QVarLengthArrayIcLi256EE

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #45

                                      @kshegunov
                                      The link you provide, for QLocaleData::numberToCLocale(), is for outputting internal numbers -> external strings. For QString::toLongLong() I am looking for function code which is for inputting external string -> internal number, and see why 0xFFFFFFFFFFFFFFFE is causing a conversion error?

                                      kshegunovK J.HilkJ 2 Replies Last reply
                                      0
                                      • JonBJ JonB

                                        @kshegunov
                                        The link you provide, for QLocaleData::numberToCLocale(), is for outputting internal numbers -> external strings. For QString::toLongLong() I am looking for function code which is for inputting external string -> internal number, and see why 0xFFFFFFFFFFFFFFFE is causing a conversion error?

                                        kshegunovK Offline
                                        kshegunovK Offline
                                        kshegunov
                                        Moderators
                                        wrote on last edited by
                                        #46

                                        It's part of the implementation - the checking part. Sorry forgot the first link x_x.
                                        See here:
                                        https://code.woboq.org/qt5/qtbase/src/corelib/tools/qlocale.cpp.html#_ZNK11QLocaleData16stringToLongLongE11QStringViewiPb6QFlagsIN7QLocale12NumberOptionEE
                                        and here:
                                        https://code.woboq.org/qt5/qtbase/src/corelib/tools/qlocale.cpp.html#_ZN11QLocaleData19bytearrayToLongLongEPKciPb

                                        Read and abide by the Qt Code of Conduct

                                        1 Reply Last reply
                                        2
                                        • JonBJ JonB

                                          @kshegunov
                                          The link you provide, for QLocaleData::numberToCLocale(), is for outputting internal numbers -> external strings. For QString::toLongLong() I am looking for function code which is for inputting external string -> internal number, and see why 0xFFFFFFFFFFFFFFFE is causing a conversion error?

                                          J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by
                                          #47

                                          @JonB
                                          no, @kshegunov is right,

                                          toLongLong calls, stringtolonglong

                                          qlonglong QLocaleData::stringToLongLong(QStringView str, int base, bool *ok,
                                                                                  QLocale::NumberOptions number_options) const
                                          {
                                              CharBuff buff;
                                              if (!numberToCLocale(str, number_options, &buff)) {
                                                  if (ok != 0)
                                                      *ok = false;
                                                  return 0;
                                              }
                                              return bytearrayToLongLong(buff.constData(), base, ok);
                                          }
                                          
                                          

                                          that calls numberToCLocale

                                          and uses the resulting char array to call bytearrayToLongLong

                                          
                                          qlonglong QLocaleData::bytearrayToLongLong(const char *num, int base, bool *ok)
                                          {
                                              bool _ok;
                                              const char *endptr;
                                              if (*num == '\0') {
                                                  if (ok != 0)
                                                      *ok = false;
                                                  return 0;
                                              }
                                              qlonglong l = qstrtoll(num, &endptr, base, &_ok);
                                              if (!_ok) {
                                                  if (ok != 0)
                                                      *ok = false;
                                                  return 0;
                                              }
                                              if (*endptr != '\0') {
                                                  // we stopped at a non-digit character after converting some digits
                                                  if (ok != 0)
                                                      *ok = false;
                                                  return 0;
                                              }
                                              if (ok != 0)
                                                  *ok = true;
                                              return l;
                                          }
                                          
                                          long long
                                          qstrtoll(const char * nptr, const char **endptr, int base, bool *ok)
                                          {
                                              *ok = true;
                                              errno = 0;
                                              char *endptr2 = 0;
                                              long long result = qt_strtoll(nptr, &endptr2, base);
                                              if (endptr)
                                                  *endptr = endptr2;
                                              if ((result == 0 || result == std::numeric_limits<long long>::min()
                                                   || result == std::numeric_limits<long long>::max())
                                                      && (errno || nptr == endptr2)) {
                                                  *ok = false;
                                                  return 0;
                                              }
                                              return result;
                                          }
                                          

                                          like I said, a rRabbit Hole


                                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                          Q: What's that?
                                          A: It's blue light.
                                          Q: What does it do?
                                          A: It turns blue.

                                          JonBJ 1 Reply Last reply
                                          2

                                          • Login

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