Reputation: 13
Using Qt, want to convert a number(digit) in QByteArray to int. Here is the code:
QByteArray ba;
ba = serial->readAll(); //ba[0] = 6;
int sum = ba[0] + 10; //want it to do this i.e 10 + 6
qDebug()<<sum; //output becomes nothing, I expected it to be 16;
How do I convert the extracted value to int so I can use it in arithmetic, as shown above.
Upvotes: 1
Views: 6324
Reputation: 966
Look at toInt
method
So you can convert it like this:
bool ok; // indicates success or failure
int sum = ba.toInt(&ok);
Alternatively you can first convert you QByteArray
to string (for example, obtaining char *
by calling data
member function and then do whatever you want with that string.
Upvotes: 1