PDA

View Full Version : How to read a Number from the QByteArray



antonio.r.tome
24th February 2006, 16:14
Hi,

I'm trying to get a integer from a QByteArray.
After scaning the documentation I would say that the fowlloing piece of code should work:
-----------
int ntime;
QByteArray stderr1 ("10");
QBuffer buffer(&stderr1);
buffer.open(QIODevice::ReadOnly);
QDataStream in(&buffer);
in >> ntime;
printf("Output value %d\n",ntime);

-------------
I would expect the output :
Output value 10
but the Output is instead:
Output value 0
what am I doing wrong?:confused:

best regards

António Tomé

jacek
24th February 2006, 16:17
QDataStream uses its own, binary format. Use QTextStream instead:
int ntime;
QByteArray stderr1 ("10");
QBuffer buffer(&stderr1);
buffer.open(QIODevice::ReadOnly | QIODevice::Text );
QTextStream in(&buffer);
in >> ntime;
printf("Output value %d\n",ntime);

antonio.r.tome
24th February 2006, 16:24
Many thanks,

It worked flawless.


António Tomé