Hi,
I am trying to read binary data into a QVector. Each chunk is represented as 2 Bytes (unsigned int).
The follow code works:
QDataStream in(m_tcpSocket);
if (m_tcpSocket->bytesAvailable() < m_dataLength ) return;
qint16 a;
QVector<double> dataVector;
for(int i=0; i<m_dataLength; i++)
{
in >> a;
dataVector.push_back(a);
}
The first five values that I get from this code are what I expect:
0 3 0 16384 -7460 -7483
But I want to avoid the loop and I'd like to get the pointer to the beginning of the data and to the end of the data and then transfer this into the memory and put it into a vector.
I tried the following:
char *s = new char[ 5 * sizeof(qint16)]; //Get only the first 8 bytes
in.readRawData( s , 5*sizeof(qint16) );
QVector<qint16> vector;
qint16 *vdata = vector.data();
vdata = (qint16*)s;
If I print out the first 5 values of the vector I get:
-21589 -21589 -21589 -21589 0
Has anyone done this before?
I'd appreciate any contribution.
Bookmarks