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 setnum behavior when inputted value is negative
Forum Updated to NodeBB v4.3 + New Features

QString setnum behavior when inputted value is negative

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 1.0k 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.
  • J JonB
    13 Mar 2023, 14:57

    @Dummie1138
    str.setNum(-1234); should presumably set the string to "-1234".

    D Offline
    D Offline
    Dummie1138
    wrote on 13 Mar 2023, 15:00 last edited by
    #3

    @JonB What about when it is not base 10? For example, in base 16, 1234 is 4D2. Will setNum return "-4D2" or something else?

    J 1 Reply Last reply 13 Mar 2023, 15:02
    0
    • D Dummie1138
      13 Mar 2023, 15:00

      @JonB What about when it is not base 10? For example, in base 16, 1234 is 4D2. Will setNum return "-4D2" or something else?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 13 Mar 2023, 15:02 last edited by
      #4

      @Dummie1138 Please read documentation: https://doc.qt.io/qt-6/qstring.html#setNum

      QString &QString::setNum(int n, int base = 10)
      

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

      D 1 Reply Last reply 13 Mar 2023, 15:04
      0
      • J jsulm
        13 Mar 2023, 15:02

        @Dummie1138 Please read documentation: https://doc.qt.io/qt-6/qstring.html#setNum

        QString &QString::setNum(int n, int base = 10)
        
        D Offline
        D Offline
        Dummie1138
        wrote on 13 Mar 2023, 15:04 last edited by
        #5

        @jsulm e247fb55-b6a9-48a7-b968-d540714051ad-image.png

        I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.

        J M 2 Replies Last reply 13 Mar 2023, 15:07
        0
        • D Dummie1138
          13 Mar 2023, 15:04

          @jsulm e247fb55-b6a9-48a7-b968-d540714051ad-image.png

          I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 13 Mar 2023, 15:07 last edited by
          #6

          @Dummie1138 why don't you test ist ?

          setting up a simple main.cpp with setNum and qdebug takes less than a minute.


          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
          2
          • D Dummie1138
            13 Mar 2023, 15:04

            @jsulm e247fb55-b6a9-48a7-b968-d540714051ad-image.png

            I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.

            M Offline
            M Offline
            mpergand
            wrote on 13 Mar 2023, 15:58 last edited by
            #7

            @Dummie1138 said in QString setnum behavior when inputted value is negative:

            I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.

            Not sure what you really want to do,
            if you want to print a negative value in hex with a minus sign in front, you can try this:

            QString negHex(int v)
            {
                if(v>=0) return QString::number(v,16);
                // negative number
                v=~v+1; // two's complement
                return QString("-%1").arg(v,0,16);
            }
            

            examples:

            qDebug()<<negHex(1234)<<negHex(-1234)<<negHex(-0x4d2);     ---> "4d2" "-4d2" "-4d2"
            
            1 Reply Last reply
            0
            • C Online
              C Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on 13 Mar 2023, 17:56 last edited by
              #8

              @Dummie1138 Negative numbers are supported in any base the same. If you pass -1234 in base 16 you will get "-4d2". As @J-Hilk said, it would've taken you less time to just try than post the question.

              @mpergand That is absolutely unnecessary. QString's number conversion functions like number and setNum handle minus sign already. No need to reinvent the wheel.

              M 1 Reply Last reply 13 Mar 2023, 18:06
              1
              • C Chris Kawa
                13 Mar 2023, 17:56

                @Dummie1138 Negative numbers are supported in any base the same. If you pass -1234 in base 16 you will get "-4d2". As @J-Hilk said, it would've taken you less time to just try than post the question.

                @mpergand That is absolutely unnecessary. QString's number conversion functions like number and setNum handle minus sign already. No need to reinvent the wheel.

                M Offline
                M Offline
                mpergand
                wrote on 13 Mar 2023, 18:06 last edited by
                #9

                @Chris-Kawa said in QString setnum behavior when inputted value is negative:

                If you pass -1234 in base 16 you will get "-4d2"

                I don't get it ...
                QString::number(-1234,16); -->"fffffffffffffb2e"

                C 1 Reply Last reply 13 Mar 2023, 18:26
                0
                • M mpergand
                  13 Mar 2023, 18:06

                  @Chris-Kawa said in QString setnum behavior when inputted value is negative:

                  If you pass -1234 in base 16 you will get "-4d2"

                  I don't get it ...
                  QString::number(-1234,16); -->"fffffffffffffb2e"

                  C Online
                  C Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 13 Mar 2023, 18:26 last edited by Chris Kawa
                  #10

                  @mpergand Sorry about that. It seems there's more to it. The documentation for the long overload of QString::number says that for bases other than 10 the argument is treated as unsigned. What that means... is kinda ambiguous. The other overloads are not documented :/
                  It does return "-4d2" on my machine (msvc x64 Qt 6.4.2) for the int overload and looking at the source code it does handle the negative numbers by converting to unsigned and adding - in al bases, so it looks like a bit of a mess.

                  M 1 Reply Last reply 13 Mar 2023, 19:13
                  0
                  • C Chris Kawa
                    13 Mar 2023, 18:26

                    @mpergand Sorry about that. It seems there's more to it. The documentation for the long overload of QString::number says that for bases other than 10 the argument is treated as unsigned. What that means... is kinda ambiguous. The other overloads are not documented :/
                    It does return "-4d2" on my machine (msvc x64 Qt 6.4.2) for the int overload and looking at the source code it does handle the negative numbers by converting to unsigned and adding - in al bases, so it looks like a bit of a mess.

                    M Offline
                    M Offline
                    mpergand
                    wrote on 13 Mar 2023, 19:13 last edited by mpergand
                    #11

                    @Chris-Kawa
                    For me, negative hex values are represented like this:

                    -1  -->  FF  (8bits)
                             FFFF (16 bits)
                             etc
                    

                    It seems things have changed in newer versions of C++ as they allow float representation in hex (strange at first thought to me)

                    Maybe Qt6 has adopted this new capabilities
                    versus Qt5 ?

                    C 1 Reply Last reply 13 Mar 2023, 19:45
                    0
                    • M mpergand
                      13 Mar 2023, 19:13

                      @Chris-Kawa
                      For me, negative hex values are represented like this:

                      -1  -->  FF  (8bits)
                               FFFF (16 bits)
                               etc
                      

                      It seems things have changed in newer versions of C++ as they allow float representation in hex (strange at first thought to me)

                      Maybe Qt6 has adopted this new capabilities
                      versus Qt5 ?

                      C Online
                      C Online
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 13 Mar 2023, 19:45 last edited by
                      #12

                      @mpergand Qt does not use standard library for number to string conversions, but the source here differs quite a lot from what I see locally in the source downloaded via online installer for Qt 6.4.2. So there seem to be some changes made, I just don't know when exactly, the easiest would probably be check the history in git, but I don't have the repo set up at hand.
                      What platform, toolset and Qt version are you using?

                      M 1 Reply Last reply 13 Mar 2023, 19:51
                      0
                      • C Chris Kawa
                        13 Mar 2023, 19:45

                        @mpergand Qt does not use standard library for number to string conversions, but the source here differs quite a lot from what I see locally in the source downloaded via online installer for Qt 6.4.2. So there seem to be some changes made, I just don't know when exactly, the easiest would probably be check the history in git, but I don't have the repo set up at hand.
                        What platform, toolset and Qt version are you using?

                        M Offline
                        M Offline
                        mpergand
                        wrote on 13 Mar 2023, 19:51 last edited by mpergand
                        #13

                        @Chris-Kawa said in QString setnum behavior when inputted value is negative:

                        What platform, toolset and Qt version are you using?

                        Qt 5.12.10, OSX 10.14 and Kubuntu 21.04

                        1 Reply Last reply
                        0

                        12/13

                        13 Mar 2023, 19:45

                        • Login

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