Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a QString of numbers to an array of int?

I have a QString containing a series of numbers for example

QString path = "11100332001 234 554 9394";

I want to iterate over the variable length string

for (int i=0; i<path.length(); i++){

}

and be able to acces each number as an int individually.

I haven't been able to do so however. Question: How can I convert a QString of numbers to an array of int?

I know I can convert the QString to an int using path.toInt() but that doesn't help me.

When trying to convert it to a char first I get an error: cannot convert 'const QChar to char'.

for (int i=0; i<path.length(); i++){
        char c = path.at(i);
        int j = atoi(&c);
        //player->movePlayer(direction);
    }
like image 804
Thomas Avatar asked Jan 05 '13 17:01

Thomas


2 Answers

you can use split to obtain an array of substrings separated by the separator you want in this case it's a space , here is an example :

QString str("123 457 89");

QStringList list = str.split(" ",QString::SkipEmptyParts);

foreach(QString num, list)
    cout << num.toInt() << endl;
like image 172
Youssef Bouhjira Avatar answered Sep 22 '22 21:09

Youssef Bouhjira


You may get every character (QChar) with the [] operator and use the digitValue() method on them to get the integer.

like image 43
antoyo Avatar answered Sep 23 '22 21:09

antoyo