Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QString/C++ convert to number



  • 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. :)


  • Lifetime Qt Champion

    Hi,

    Where are you getting these string from ?



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


  • Qt Champions 2017

    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.


  • Lifetime Qt Champion

    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.



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



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


  • Qt Champions 2017

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


  • Lifetime Qt Champion

    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



  • @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++... ;-)


  • Lifetime Qt Champion

    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



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


  • Qt Champions 2017

    @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. :)



  • @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()?



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

    [-+]?(\d+)%


Log in to reply