QString/C++ convert to number
-
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 instdlib
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. -
@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.
-
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 theQString::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()
orstrtol()
now. Might as well have stuck to C as ever move to C++... ;-) -
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 thanscanf
...).The only problem is, that you have to convert your
QString toLatin1()
first.Regards
-
-
@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
acceptsconst char *
in a constructor it would have an implicit "toconst 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 yourtoLatin1()
? -
it's a bit heavy weight but I always like a good RE engine for parsing most things.
[-+]?(\d+)%