How to extract integers from a QByteArray in Qt using toInt?

c++, qt

Solution

int num = datagram.right(datagram.size() - datagram.indexOf(':') - 1).toInt();

Problem

I am a beginner with C++ and Qt. The data sent is a string of ASCII characters ex:"jdlsfjffjf: XX" where I would like to extract the number XX. I know I should possibly use indexof to point to it but not sure how. Any direction? Here's the server side code that receives, displays and writes. I get the correct numbers in the application but gibberish characters in the file I'm writing to. ``` void Receiver::processPendingDatagrams() { while (udpSocket->hasPendingDatagrams()) { QByteArray datagram; //array of bytes datagram.resize(udpSocket->pendingDatagramSize()); //size it depending on sent data udpSocket->readDatagram(datagram.data(), datagram.size()); //read all statusLabel->setText(tr("%1 C").arg(datagram.data())); //writing stream to file bool ok; QFile file("file.dat"); file.open(QIODevice::WriteOnly); QDataStream out(&file); out << datagram.toInt(&ok, 10 ); } ```

Original source