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;
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
) );
int readNumPts , readNumCells;
streamOut >> readNumPts >> readNumCells;
std::cout<<readNumPts<<" "<<readNumCells<<std::endl;
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;
To copy to clipboard, switch view to plain text mode
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;
int sizeOfChar = sizeof( char );
int sizeOfInt = sizeof( int );
streamIn << numPts << numCells;
int readNumPts , readNumCells;
streamOut >> readNumPts >> readNumCells;
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;
To copy to clipboard, switch view to plain text mode
Bookmarks