PDA

View Full Version : storing integer 4bytes in QByteArray



babu198649
30th November 2008, 07:02
hi
i want to store the integers in QByteArray as raw bytes.but the QByteArray converts it to character and then stores.

QByteArray ba;
ba.append(QByteArray::number((qint32)50));
qDebug()<<ba.size();//Prints 2 (i expect the size of qint32. ie.4)

How to store integer as raw bytes.

Thank u

wysota
30th November 2008, 08:54
If you use QByteArray::number then your number gets converted to string, I don't see anything unusual in getting the size of the resulting array as 2 (characters "5" and "0"). If you want to store the hex values of each byte of the variable then either use memcpy() or copy values byte by byte into the array.

ktk
30th November 2008, 10:08
hi
i want to store the integers in QByteArray as raw bytes.but the QByteArray converts it to character and then stores.

QByteArray ba;
ba.append(QByteArray::number((qint32)50));
qDebug()<<ba.size();//Prints 2 (i expect the size of qint32. ie.4)
How to store integer as raw bytes.

Thank u

The following might do the trick:


quint32 number = 50;
QByteArray ba((const char *)&number, sizeof(number));

Note the result depends on the endian-ness of your machine and using a C++ cast instead of the C cast might be more appropriare.