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::toShort problem
Forum Updated to NodeBB v4.3 + New Features

QString::toShort problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
58 Posts 7 Posters 17.8k 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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    According to https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ the 'HexString Input' 0014 is the value 20 in INT16 Big Endian representation. And yes, this works:

    QString str1 = "0014";
    bool ok1;
    short hex1 = str1.toShort(&ok1, 16);
    

    According to the same website, the 'HexString Input' FFFE is the value -2 in INT16 Big Endian representation. However, with my Qt 4.8.7 and for the conversion

    QString str2 = "FFFE";
    bool ok2;
    short hex2 = str2.toShort(&ok2, 16);
    

    hex2 has the value 0 and ok2 is false, so the conversion failed. Why don't I get -2 and how should I convert "FFFE" so that it results in -2?

    kshegunovK 1 Reply Last reply
    0
    • B Bart_Vandewoestyne

      According to https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ the 'HexString Input' 0014 is the value 20 in INT16 Big Endian representation. And yes, this works:

      QString str1 = "0014";
      bool ok1;
      short hex1 = str1.toShort(&ok1, 16);
      

      According to the same website, the 'HexString Input' FFFE is the value -2 in INT16 Big Endian representation. However, with my Qt 4.8.7 and for the conversion

      QString str2 = "FFFE";
      bool ok2;
      short hex2 = str2.toShort(&ok2, 16);
      

      hex2 has the value 0 and ok2 is false, so the conversion failed. Why don't I get -2 and how should I convert "FFFE" so that it results in -2?

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

      Use QString::toUShort:

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

      Read and abide by the Qt Code of Conduct

      B 1 Reply Last reply
      6
      • kshegunovK kshegunov

        Use QString::toUShort:

        QString str2 = "FFFE";
        bool ok2;
        short hex2 = static_cast<short>(str2.toUShort(&ok2, 16));
        
        B Offline
        B Offline
        Bart_Vandewoestyne
        wrote on last edited by
        #3

        @kshegunov said in QString::toShort problem:

        Use QString::toUShort:

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

        Nice! That seems to work, but I have no clue why it works :-) What I don't understand is that the value -2 is a SIGNED integer with representation FFFE, so why is it then necessary to first interpret the FFFE representation as that for an UNSIGNED short, and then cast to short??? Is that a workaround for a bug in QString::toShort? Or should I freshen up my knowledge of two's complement to understand this trick? ;-)

        JonBJ 1 Reply Last reply
        0
        • B Bart_Vandewoestyne

          @kshegunov said in QString::toShort problem:

          Use QString::toUShort:

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

          Nice! That seems to work, but I have no clue why it works :-) What I don't understand is that the value -2 is a SIGNED integer with representation FFFE, so why is it then necessary to first interpret the FFFE representation as that for an UNSIGNED short, and then cast to short??? Is that a workaround for a bug in QString::toShort? Or should I freshen up my knowledge of two's complement to understand this trick? ;-)

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

          @Bart_Vandewoestyne
          There appears to be a difference in behaviour between toShort() and toUShort() which the documentation does not mention and @kshegunov is fiendlishly taking advantage of with internal knowledge :)

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            FFFE is simply to big for a signed short ...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            B 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              FFFE is simply to big for a signed short ...

              B Offline
              B Offline
              Bart_Vandewoestyne
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in QString::toShort problem:

              FFFE is simply to big for a signed short ...

              Why? Considering Visual Studio 2015 (and I assume also a lot of other compilers), the range for (signed) short is –32,768 to 32,767 (see https://msdn.microsoft.com/nl-be/library/s3f49ktz.aspx). The value -2 (represented by FFFE = two's complement) fals nicely into that range. So that's why I was expecting to be able to go from "FFFE" to -2 using QString::toShort()...

              JonBJ 1 Reply Last reply
              0
              • B Bart_Vandewoestyne

                @Christian-Ehrlicher said in QString::toShort problem:

                FFFE is simply to big for a signed short ...

                Why? Considering Visual Studio 2015 (and I assume also a lot of other compilers), the range for (signed) short is –32,768 to 32,767 (see https://msdn.microsoft.com/nl-be/library/s3f49ktz.aspx). The value -2 (represented by FFFE = two's complement) fals nicely into that range. So that's why I was expecting to be able to go from "FFFE" to -2 using QString::toShort()...

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

                @Bart_Vandewoestyne
                Because 0xFFFE is 65,534. It fits in an unsigned short range, but overflows the signed short's maximum positive value of 32,767. That's what @Christian-Ehrlicher is saying.

                You are assuming that QString::toShort() will treat the string 0xFFFE as meaning exactly the same thing as -2, but it doesn't. It regards it as a positive number which is beyond the range of signed shorts, not as an alternative way of writing -2.

                jsulmJ 1 Reply Last reply
                5
                • JonBJ JonB

                  @Bart_Vandewoestyne
                  Because 0xFFFE is 65,534. It fits in an unsigned short range, but overflows the signed short's maximum positive value of 32,767. That's what @Christian-Ehrlicher is saying.

                  You are assuming that QString::toShort() will treat the string 0xFFFE as meaning exactly the same thing as -2, but it doesn't. It regards it as a positive number which is beyond the range of signed shorts, not as an alternative way of writing -2.

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

                  @JonB I disagree.
                  Complement on two for 2:

                    0000 0010
                    1111 1101
                  + 0000 0001
                    1111 1110
                  

                  So, -2 is 1111 1110 or 0xFE - why should this not feet into a signed short?
                  "You are assuming that QString::toShort() will treat the string 0xFFFE as meaning exactly the same thing as -2, but it doesn't" - why should toShort() not treat 0xFE as -2?

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

                  kshegunovK 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @JonB I disagree.
                    Complement on two for 2:

                      0000 0010
                      1111 1101
                    + 0000 0001
                      1111 1110
                    

                    So, -2 is 1111 1110 or 0xFE - why should this not feet into a signed short?
                    "You are assuming that QString::toShort() will treat the string 0xFFFE as meaning exactly the same thing as -2, but it doesn't" - why should toShort() not treat 0xFE as -2?

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

                    @jsulm said in QString::toShort problem:

                    @JonB I disagree.

                    You shouldn't. ;)

                    why should toShort() not treat 0xFE as -2?

                    Quite simply because you don't have a fixed-size data field to work with as input. Why should toShort assume that you meant exactly the binary representation. You could've just as well had a data that's too big to fit the type. Say I'm reading some input and I'm trying to get it into a short. Suddenly due to an error or by whatever chance I get a number that's too big for my short, but instead of overflowing the toShort would give me an invalid value. It doesn't make sense that the person who implemented toShort would just jump the gun on such an assumption.
                    And lastly, what should we do with overflows of this kind - 0x100FF, shall toShort return 255 in this case?

                    Read and abide by the Qt Code of Conduct

                    jsulmJ 1 Reply Last reply
                    2
                    • kshegunovK kshegunov

                      @jsulm said in QString::toShort problem:

                      @JonB I disagree.

                      You shouldn't. ;)

                      why should toShort() not treat 0xFE as -2?

                      Quite simply because you don't have a fixed-size data field to work with as input. Why should toShort assume that you meant exactly the binary representation. You could've just as well had a data that's too big to fit the type. Say I'm reading some input and I'm trying to get it into a short. Suddenly due to an error or by whatever chance I get a number that's too big for my short, but instead of overflowing the toShort would give me an invalid value. It doesn't make sense that the person who implemented toShort would just jump the gun on such an assumption.
                      And lastly, what should we do with overflows of this kind - 0x100FF, shall toShort return 255 in this case?

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

                      @kshegunov said in QString::toShort problem:

                      Why should toShort assume that you meant exactly the binary representation

                      Maybe I'm still sleeping and oversee something. What else should it assume? If I say its hex and pass FFFE - how does toShort() interpret it?
                      0x100FF is too big for a short and toShort() should return 0/false (and it does). But FFFE is a valid signed short number.

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

                      B kshegunovK 2 Replies Last reply
                      0
                      • jsulmJ jsulm

                        @kshegunov said in QString::toShort problem:

                        Why should toShort assume that you meant exactly the binary representation

                        Maybe I'm still sleeping and oversee something. What else should it assume? If I say its hex and pass FFFE - how does toShort() interpret it?
                        0x100FF is too big for a short and toShort() should return 0/false (and it does). But FFFE is a valid signed short number.

                        B Offline
                        B Offline
                        Bart_Vandewoestyne
                        wrote on last edited by
                        #11

                        @jsulm said in QString::toShort problem:

                        @kshegunov said in QString::toShort problem:

                        Why should toShort assume that you meant exactly the binary representation

                        Maybe I'm still sleeping and oversee something. What else should it assume? If I say its hex and pass FFFE - how does toShort() interpret it?
                        0x100FF is too big for a short and toShort() should return 0/false (and it does). But FFFE is a valid signed short number.

                        @jsulm I completely agree! (although I have the same feeling about sleeping and maybe overseeing something ;-) Maybe it's time to dive into the Qt 4.8.7 source and investigate why QString::toShort() is failing on "FFFE"? (does Qt 5.X also fail on that btw?)

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @kshegunov said in QString::toShort problem:

                          Why should toShort assume that you meant exactly the binary representation

                          Maybe I'm still sleeping and oversee something. What else should it assume? If I say its hex and pass FFFE - how does toShort() interpret it?
                          0x100FF is too big for a short and toShort() should return 0/false (and it does). But FFFE is a valid signed short number.

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

                          @jsulm said in QString::toShort problem:

                          But FFFE is a valid signed short number.

                          No it isn't, and that's the point. Start doing the math in your head and see for yourself:

                          E * 1 + F * 16 + F * 16^2 + F * 16^3
                          

                          And the last term overflows, which overflow is caught and voila!
                          If you have

                          char z = 127;
                          

                          then:

                          z += 1;
                          

                          Is overflowing, no matter whether the value you get is "correct".

                          Read and abide by the Qt Code of Conduct

                          jsulmJ 1 Reply Last reply
                          2
                          • kshegunovK kshegunov

                            @jsulm said in QString::toShort problem:

                            But FFFE is a valid signed short number.

                            No it isn't, and that's the point. Start doing the math in your head and see for yourself:

                            E * 1 + F * 16 + F * 16^2 + F * 16^3
                            

                            And the last term overflows, which overflow is caught and voila!
                            If you have

                            char z = 127;
                            

                            then:

                            z += 1;
                            

                            Is overflowing, no matter whether the value you get is "correct".

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

                            @kshegunov I still don't get it.
                            What is the representation of -2 as signed short? Isn't it 0xFFFE?

                              0000 0000 0000 0010 - 2
                              1111 1111 1111 1101 - invert
                            + 0000 0000 0000 0001 - add 1
                              1111 1111 1111 1110
                            -> 0xFFFE
                            

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

                            1 Reply Last reply
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by JKSH
                              #14

                              OK, here's an exercise to settle the debate. First, assume that QString::toShort() behaves exactly as you expect.

                              What should each QString (p_*) be initialized to, in order to get 32 for every output line?

                              QString p_oct, p_dec, p_hex, p_r32;
                              
                              // ... Initialize QStrings here ...
                              
                              qDebug() << p_dec.toShort(nullptr, 10); // Returns 32
                              qDebug() << p_hex.toShort(nullptr, 16); // Returns 32
                              
                              qDebug() << p_oct.toShort(nullptr, 8);  // Returns 32
                              qDebug() << p_r32.toShort(nullptr, 32); // Returns 32
                              

                              Next, what should each QString (n_*) be initialized to, in order to get -32 for every output line?

                              QString n_oct, n_dec, n_hex, n_r32;
                              
                              // ... Initialize QStrings here ...
                              
                              qDebug() << n_oct.toShort(nullptr, 8);  // Returns -32
                              qDebug() << n_dec.toShort(nullptr, 10); // Returns -32
                              qDebug() << n_hex.toShort(nullptr, 16); // Returns -32
                              qDebug() << n_r32.toShort(nullptr, 32); // Returns -32
                              

                              Decide on your answer for all 8 strings first, then post your answer here.

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0
                              • JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @jsulm , @Bart_Vandewoestyne

                                I don't get what you don't get about: 0xFFFE is a positive overflow for parsing & storing into a ushort. Hence the behaviour.

                                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.

                                Nobody has looked at it "the other way round". I cannot test because I am Python/PyQt not C++, but what does

                                QString("-2").toUShort(&ok, 16)
                                

                                return? In your theory it should be 0xFFFE, but I am "hoping"(!) it returns an error, just like QString("FFFE").toShort(&ok, 16) does?

                                Assuming that is the case, this means we do not have an ambiguity/duplication, whereby both FFFE and -2 strings can be parsed as the same number by toShort()/toUShort() (but 2 is the only way to write +2).

                                jsulmJ 1 Reply Last reply
                                0
                                • J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  toShort makes a toLongLong interpretation first and than casts it to short theres where the "error" comes from:

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

                                  toLongLong will return ‭65534‬, (0xFFFE in int64 is positve after all), and that is bigger than SHRT_MAX -> 0 and failed conversion


                                  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
                                  • JonBJ JonB

                                    @jsulm , @Bart_Vandewoestyne

                                    I don't get what you don't get about: 0xFFFE is a positive overflow for parsing & storing into a ushort. Hence the behaviour.

                                    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.

                                    Nobody has looked at it "the other way round". I cannot test because I am Python/PyQt not C++, but what does

                                    QString("-2").toUShort(&ok, 16)
                                    

                                    return? In your theory it should be 0xFFFE, but I am "hoping"(!) it returns an error, just like QString("FFFE").toShort(&ok, 16) does?

                                    Assuming that is the case, this means we do not have an ambiguity/duplication, whereby both FFFE and -2 strings can be parsed as the same number by toShort()/toUShort() (but 2 is the only way to write +2).

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

                                    @JonB said in QString::toShort problem:

                                    In your theory it should be 0xFFFE

                                    No, it would not, because -2 is not a hex number...
                                    "I don't get what you don't get about: 0xFFFE is a positive overflow for parsing & storing into a ushort" - we are not talking about unsigned short, but signed short and 0xFFFE is the representation of -2.

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

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

                                      toShort makes a toLongLong interpretation first and than casts it to short theres where the "error" comes from:

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

                                      toLongLong will return ‭65534‬, (0xFFFE in int64 is positve after all), and that is bigger than SHRT_MAX -> 0 and failed conversion

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

                                      @J.Hilk
                                      In that case, try passing something like 0xFFFFFFFE or 0xFFFFFFFFFFFFFFFE for the string to toShort() and those who want -2 instead of error should get it?!

                                      jsulmJ J.HilkJ 2 Replies Last reply
                                      1
                                      • jsulmJ jsulm

                                        @JonB said in QString::toShort problem:

                                        In your theory it should be 0xFFFE

                                        No, it would not, because -2 is not a hex number...
                                        "I don't get what you don't get about: 0xFFFE is a positive overflow for parsing & storing into a ushort" - we are not talking about unsigned short, but signed short and 0xFFFE is the representation of -2.

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

                                        @jsulm

                                        No, it would not, because -2 is not a hex number...

                                        Yes it is! It's as much a hex number as some other base.

                                        1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @J.Hilk
                                          In that case, try passing something like 0xFFFFFFFE or 0xFFFFFFFFFFFFFFFE for the string to toShort() and those who want -2 instead of error should get it?!

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

                                          @JonB said in QString::toShort problem:

                                          In that case, try passing something like 0xFFFFFFFE or 0xFFFFFFFFFFFFFFFE for the string to toShort()

                                          Come on - these numbers are NOT short. We should stay on topic.

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

                                          JonBJ 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