sunset
sunset

Reputation: 1413

how to convert Qstring to Long?

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

Answers (3)

Sami Kanafani
Sami Kanafani

Reputation: 15751

long s; 
QString x="6458621237";
s = x.toLong();

Upvotes: 0

user195488
user195488

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

Richard Inglis
Richard Inglis

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

Related Questions