Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QString/C++ convert to number
Forum Updated to NodeBB v4.3 + New Features

QString/C++ convert to number

Scheduled Pinned Locked Moved Solved C++ Gurus
15 Posts 5 Posters 4.8k Views 4 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Where are you getting these string from ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    JonBJ 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Where are you getting these string from ?

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

      @SGaist
      Strange question! They do not come from user input, so no chance of dealing them there, if that's what you had in mind. They are just program data: In-memory strings, or from file, as it happens they can also be in a design-time table which is already created with strings.

      I just want to know the best way to convert a string starting with a number but possibly running onto further arbitrary characters is, please? QString::toInt() would have done the job if it didn't error on encountering trailing characters, but it does. I was thinking earlier: in C long int atol(const char *nptr);, and that family of
      functions, does just what I want. I now see that lives in C++ stdlib. I suspect if I use that the C++ Thought Police on SO & elsewhere would want me boiled? Should I use that, or is there a better C++/Qt function?

      1 Reply Last reply
      0
      • JonBJ JonB

        The strings I wish to convert to integer include "-20%" and "+10%". In other cases, there might be some other characters after the digits to be ignored.

        QString::toInt(bool *ok = nullptr, int base = 10) barfs at these, because of the trailing %. I was kind of hoping there might be an equivalent to QString::asprintf but for scanf, but alas no (not even sure that would have helped).

        So... what would you like me to use in the modern world for string to number conversion, where it is base 10, optionally starts with a sign and is followed by digits (plus decimal place if we have a float version), but stops at the first non-convertible character like %, without erroring? Prizes will be given for the neatest (shortest, quickest) solution, as determined by me, with no refunds/appeals. An answer requiring regular expression will be sniffed at, as likely too expensive. :)

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

        Write yourself a proper LL(1) parser and you're not going to have no problems. ;)
        Jest aside, I'd just go with a pre-prepared regex as it's easiest to use and quite straightforward.

        Read and abide by the Qt Code of Conduct

        JonBJ 1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          See, you have strings coming from three different sources so not so a strange question ;-)

          As for your issue, if you have a set of methods that do exactly what you want then go on and use them if they have no equivalent in a Qt. There's nothing wrong with using the stdlib if it has what you need.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          2
          • SGaistS SGaist

            See, you have strings coming from three different sources so not so a strange question ;-)

            As for your issue, if you have a set of methods that do exactly what you want then go on and use them if they have no equivalent in a Qt. There's nothing wrong with using the stdlib if it has what you need.

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

            @SGaist
            That's OK, you usually answer my question with a question ;-)

            At the time I raised the question, I had not thought of atol and I didn't know it was in stdlib in C++. So you are saying that's OK for me in 2020? If I get shouted at elsewhere I may quote you on this?

            1 Reply Last reply
            0
            • kshegunovK kshegunov

              Write yourself a proper LL(1) parser and you're not going to have no problems. ;)
              Jest aside, I'd just go with a pre-prepared regex as it's easiest to use and quite straightforward.

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

              @kshegunov said in QString/C++ convert to number:

              I'd just go with a pre-prepared regex as it's easiest to use and quite straightforward.

              Speechless. I do not want to use a regular expression to parse a string when all I want is a function to read some digits, like QString::toInt(). I am shocked that you are prepared to advocate this. We're not going to agree.

              Are you also OK with me using atol(), if there's nothing more appropriate? Which I had thought there would be.

              kshegunovK 1 Reply Last reply
              0
              • JonBJ JonB

                @kshegunov said in QString/C++ convert to number:

                I'd just go with a pre-prepared regex as it's easiest to use and quite straightforward.

                Speechless. I do not want to use a regular expression to parse a string when all I want is a function to read some digits, like QString::toInt(). I am shocked that you are prepared to advocate this. We're not going to agree.

                Are you also OK with me using atol(), if there's nothing more appropriate? Which I had thought there would be.

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

                @JonB said in QString/C++ convert to number:

                We're not going to agree.

                Like this is something new ...

                Are you also OK with me using atol(), if there's nothing more appropriate?

                Absolutely.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                3
                • JonBJ JonB

                  The strings I wish to convert to integer include "-20%" and "+10%". In other cases, there might be some other characters after the digits to be ignored.

                  QString::toInt(bool *ok = nullptr, int base = 10) barfs at these, because of the trailing %. I was kind of hoping there might be an equivalent to QString::asprintf but for scanf, but alas no (not even sure that would have helped).

                  So... what would you like me to use in the modern world for string to number conversion, where it is base 10, optionally starts with a sign and is followed by digits (plus decimal place if we have a float version), but stops at the first non-convertible character like %, without erroring? Prizes will be given for the neatest (shortest, quickest) solution, as determined by me, with no refunds/appeals. An answer requiring regular expression will be sniffed at, as likely too expensive. :)

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi @JonB,

                  I know that problem, and therefore I created QTBUG-66115. You might want to comment and vote there ;) Till a resolution is done, using a regexp might be the simplest thing to use.

                  Regards

                  Qt has to stay free or it will die.

                  JonBJ 1 Reply Last reply
                  3
                  • aha_1980A aha_1980

                    Hi @JonB,

                    I know that problem, and therefore I created QTBUG-66115. You might want to comment and vote there ;) Till a resolution is done, using a regexp might be the simplest thing to use.

                    Regards

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

                    @aha_1980
                    Ah ha! A kindred spirit, who recognises this limitation in the QString::toInt() etc. methods. FWIW I have appended my comment into the bug request.

                    I should rather burn in Hell than resort to a regular expression to solve this! I shall change to using the stdlib atoi() or strtol() now. Might as well have stuck to C as ever move to C++... ;-)

                    aha_1980A 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @aha_1980
                      Ah ha! A kindred spirit, who recognises this limitation in the QString::toInt() etc. methods. FWIW I have appended my comment into the bug request.

                      I should rather burn in Hell than resort to a regular expression to solve this! I shall change to using the stdlib atoi() or strtol() now. Might as well have stuck to C as ever move to C++... ;-)

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Hi @JonB,

                      Might as well have stuck to C as ever move to C++... ;-)

                      Nah, C is not that bad, and atol is hard to misuse (at least harder than scanf...).

                      The only problem is, that you have to convert your QString toLatin1() first.

                      Regards

                      Qt has to stay free or it will die.

                      JonBJ 2 Replies Last reply
                      1
                      • aha_1980A aha_1980

                        Hi @JonB,

                        Might as well have stuck to C as ever move to C++... ;-)

                        Nah, C is not that bad, and atol is hard to misuse (at least harder than scanf...).

                        The only problem is, that you have to convert your QString toLatin1() first.

                        Regards

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

                        @aha_1980

                        convert your QString toLatin1() first

                        Fortunately, I don't. I speak Latin, and I only need to support other Latin speakers, you don't speak Latin then you lose... :)

                        EDIT OIC, I was wrong about this, see next post below!

                        kshegunovK 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @aha_1980

                          convert your QString toLatin1() first

                          Fortunately, I don't. I speak Latin, and I only need to support other Latin speakers, you don't speak Latin then you lose... :)

                          EDIT OIC, I was wrong about this, see next post below!

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

                          @JonB said in QString/C++ convert to number:

                          Fortunately, I don't. I speak Latin, and I only need to support other Latin speakers, you don't speak Latin then you lose... :)

                          The problem, as usual, is the locales, where each country has its twisted way of writing numbers - decimal separators and such. Hence André's humongous patch. :)

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • aha_1980A aha_1980

                            Hi @JonB,

                            Might as well have stuck to C as ever move to C++... ;-)

                            Nah, C is not that bad, and atol is hard to misuse (at least harder than scanf...).

                            The only problem is, that you have to convert your QString toLatin1() first.

                            Regards

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

                            @aha_1980 said in QString/C++ convert to number:

                            The only problem is, that you have to convert your QString toLatin1() first.

                            OIC, I was wrong about this. I assumed that given that QString accepts const char * in a constructor it would have an implicit "to const char *" conversion operator. Damn these multiple languages... :)

                            I suspect I've asked this before, but: you recommend QString toLatin1(), but in docs I read:

                            You can also pass string literals to functions that take QStrings as arguments, invoking the QString(const char *) constructor. Similarly, you can pass a QString to a function that takes a const char * argument using the qPrintable() macro which returns the given QString as a const char *. This is equivalent to calling <QString>.toLocal8Bit().constData().

                            So I plan to use qPrintable(), i.e. QString::toLocal8Bit().constData() rather than your toLatin1()?

                            1 Reply Last reply
                            0
                            • Kent-DorfmanK Offline
                              Kent-DorfmanK Offline
                              Kent-Dorfman
                              wrote on last edited by
                              #15

                              it's a bit heavy weight but I always like a good RE engine for parsing most things.

                              [-+]?(\d+)%

                              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