Reputation: 1413
I am new to Qt Creator. I would like to convert a QString
value into a long
number. How would I do this?
long s;
QString x = "6458621237";
EDIT
As a result I'll have long s = 6458621237
;
Upvotes: 8
Views: 14319
Reputation:
Use the toLong function.
For example,
QString str = "FF";
bool ok;
long hex = str.toLong(&ok, 16); // hex == 255, ok == true
long dec = str.toLong(&ok, 10); // dec == 0, ok == false
Upvotes: 13
Reputation: 5958
From the QT Docs:
long QString::toLong ( bool * ok = 0, int base = 10 ) const
Returns the string converted to a long using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules: If the string begins with "0x", it is assumed to be hexadecimal; If it begins with "0", it is assumed to be octal; Otherwise it is assumed to be decimal. Returns 0 if the conversion fails.
If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
Upvotes: 5