QBytearray to character array conversion
-
Hi Frnds I am new to Qt and want to know how to convert QBytearray to character array so that i can take individual character and do processing. Below is my code which receives data a 14 Byte data into
QBytearray. Now i want to extract each character of the data and do processing.
Plz tell me how to do that. Thank u all in advance.@ if(udpSocketRxCDP->hasPendingDatagrams())
{
int theSize = udpSocketRxCDP->pendingDatagramSize();
QByteArray datagram;
datagram.resize(theSize);
int dgsize = datagram.size();
udpSocketRxCDP->readDatagram(datagram.data(), dgsize);}@
-
You can use QString::at(), QByteArray::at() or convert to c array using datagram.constData();
-
QByteArray is already suitable to do that. Why do you need the data as a char[]?
-
Hi, you already used the conversion in your source code: datagram.data()
[quote author="chandradeo" date="1375351883"]
@ if(udpSocketRxCDP->hasPendingDatagrams())
{
int theSize = udpSocketRxCDP->pendingDatagramSize();
QByteArray datagram;
datagram.resize(theSize);
int dgsize = datagram.size();
udpSocketRxCDP->readDatagram(datagram.data(), dgsize);}@[/quote]
-
[quote author="Andre" date="1375450751"]QByteArray is already suitable to do that. Why do you need the data as a char[]?[/quote]
Hi Andre can u please give an example of extracting individual data elements
from QBytearray. Actuall my data received is like ss;mm;hh and i want to display second min and hour in separate EditText. -
[quote author="1+1=2" date="1375451238"]Hi, you already used the conversion in your source code: datagram.data()
[quote author="chandradeo" date="1375351883"]
@ if(udpSocketRxCDP->hasPendingDatagrams())
{
int theSize = udpSocketRxCDP->pendingDatagramSize();
QByteArray datagram;
datagram.resize(theSize);
int dgsize = datagram.size();
udpSocketRxCDP->readDatagram(datagram.data(), dgsize);}@[/quote]
[/quote]
can you please tell me how shall i extract each elements from datagram.data.Actuall my data received is ss;mm;hh and i want to display
hour min and sec in seperate EditText. -
[quote author="chandradeo" date="1375451788"]
[quote author="Andre" date="1375450751"]QByteArray is already suitable to do that. Why do you need the data as a char[]?[/quote]Hi Andre can u please give an example of extracting individual data elements
from QBytearray. Actuall my data received is like ss;mm;hh and i want to display second min and hour in separate EditText.
[/quote]I've already told you: use QByteArray::at().
But for your usecase, much better is QString::split():
@
QString myData(datagram);
QStringList myParts = myData.split(";");// Now myParts.at(0) == "ss"
// myParts.at(1) == "mm"
// myPart.at(2) == "hh"
@