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
;
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
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.
long s;
QString x="6458621237";
s = x.toLong();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.