PDA

View Full Version : reading wrong data from QByteArray



avanindra
22nd February 2013, 16:12
Hi all,

This is short code , which is not giving me what I expect. I dump two integers ( 4 , 8 ) in a QByteArray and when I read them with QDataStream I am getting unexpected values -- 67108864 , 134217728.
What am I doing wrong here?...




int numPts = 4;
int numCells = 8;
QByteArray array;

int sizeOfChar = sizeof( char );
int sizeOfInt = sizeof( int );

array.append( QByteArray::fromRawData ( reinterpret_cast<const char*>( &numPts ), sizeOfInt / sizeOfChar ) );
array.append( QByteArray::fromRawData ( reinterpret_cast<const char*>( &numCells ), sizeOfInt / sizeOfChar ) );

QDataStream streamOut( &array , QIODevice::ReadOnly );

int readNumPts , readNumCells;

streamOut >> readNumPts >> readNumCells;

std::cout<<readNumPts<<" "<<readNumCells<<std::endl;


Regards
Avanindra Singh

I still don't know the reason for the above code not working , but writing data with QDataStream into QByteArray gave correct results.




int numPts = 4;
int numCells = 8;

QByteArray array;

int sizeOfChar = sizeof( char );
int sizeOfInt = sizeof( int );


QDataStream streamIn( &array , QIODevice::WriteOnly );

streamIn << numPts << numCells;

QDataStream streamOut( &array , QIODevice::ReadOnly );

int readNumPts , readNumCells;

streamOut >> readNumPts >> readNumCells;

wysota
22nd February 2013, 16:22
QDataStream is not a general purpose binary stream. It is a serialization mechanism.

ChrisW67
23rd February 2013, 07:25
Two words; byte order.

amleto
23rd February 2013, 14:06
Two words; byte order.

one word: Endianness :eek: