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

QString setnum behavior when inputted value is negative

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 1.4k 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    What is the expected behavior when the inputted value of a QString is negative, and setnum is operated on it?

    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      What is the expected behavior when the inputted value of a QString is negative, and setnum is operated on it?

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

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

      Dummie1138D 1 Reply Last reply
      1
      • JonBJ JonB

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

        Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on 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?

        jsulmJ 1 Reply Last reply
        0
        • Dummie1138D Dummie1138

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

          Dummie1138D 1 Reply Last reply
          0
          • jsulmJ jsulm

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

            QString &QString::setNum(int n, int base = 10)
            
            Dummie1138D Offline
            Dummie1138D Offline
            Dummie1138
            wrote on 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.HilkJ M 2 Replies Last reply
            0
            • Dummie1138D Dummie1138

              @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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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
              • Dummie1138D Dummie1138

                @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 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
                • Chris KawaC Online
                  Chris KawaC Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 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
                  1
                  • Chris KawaC Chris Kawa

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

                    Chris KawaC 1 Reply Last reply
                    0
                    • M mpergand

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

                      Chris KawaC Online
                      Chris KawaC Online
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 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
                      0
                      • Chris KawaC Chris Kawa

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

                        Chris KawaC 1 Reply Last reply
                        0
                        • M mpergand

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

                          Chris KawaC Online
                          Chris KawaC Online
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on 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
                          0
                          • Chris KawaC Chris Kawa

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

                            • Login

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