tmoeti
tmoeti

Reputation: 13

QT How to convert QByteArray number to int;

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

Answers (1)

Yuriy Ivaskevych
Yuriy Ivaskevych

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

Related Questions