Stack Exchange
how to get structure from QByteArray
i have a TCP Communication application in C# and other in C++ Qt Program on linux
But after communication i get data from C# application in C++ QByteArray.
 now QByteArray needs to be converted back to struct
How can i get data in my_struct in QT.
i have used
QDataStream in(&data, QIODevice::ReadOnly);
int parID;
QString parMessage;
in >> parID >>parMessage;

OR
const my_struct* my_struct = reinterpret_cast<const my_struct* >(data.constData());
// display
qDebug() << " id :"<<my_struct->id<< " msg: "<< my_struct->msg;



struct my_struct
{
  int id;
  string msg;
}


QDataStream uses own serialization/deserialization format. It looks like your data isnt serialized using QDataStream. To solve the issue you can send data in json/xml/etc.

That's not quite correct. QDataStream certainly uses its own simple format for complex Qt data types. For simple types, such as numerical types, there's really nothing much to it: you select the endianness and are good to go.

It depends on how it was sent. You need to be sure of the formatting on both side before you can properly communicate.
Sending a String is tricky your best bet would be to send the length of the string (as a 32-bit unsinged int) and then the data (in the utf8 format) and when you read you can do 
char* s;
int length;
in.readData(s, length);
QString str = QString::fromUtf8(s, length);
delete[] s;//free the allocated buffer

