2017-01-31 583 views
1

使用Qt,想要將QByteArray中的數字(數字)轉換爲int。 下面是代碼:QT如何將QByteArray數字轉換爲int;

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; 

我如何將所提取的數值爲int,所以我可以在算術使用它,如上圖所示。通過調用data成員函數

bool ok;     // indicates success or failure 
int sum = ba.toInt(&ok); 

另外,您可以QByteArray首先轉換您的字符串(例如,獲得char *然後執行:

+1

你得到了一個錯字號/總和。「qDebug()<< ba [0]」打印什麼? –

+0

謝謝,糾正。還是downvote? – tmoeti

回答

1

toInt方法

所以,你可以將它轉換這樣的無論你想用那個字符串什麼都可以。

+0

謝謝Ivaskevych,它工作得很好。 – tmoeti

+0

@tmoeti如果這解決了您的問題,請接受爲答覆 –

+1

@FlorentUguet如何接受答案? OK完成。 – tmoeti