PDA

View Full Version : QByteArray char to int



pdoria
15th July 2009, 17:34
Hi,

I have the following code:



quint64 input251 = Q_UINT64_C(2161727824133970762);
QByteArray in251 = QByteArray::fromRawData((char *)&input251, sizeof(input251));


the first 4 bytes are a time value (to be processed later)
the next 4 are an id value.

in251 holds the value: "J[²

This piece of code:


bool ok;
msgTime = in251.left(4).toUInt(&ok);
if (!ok) printf ("Conversion ERROR!\r\n" );


always returns "Conversion error" and msgTime, of course, is set to zero.

Why can't .toUint() convert those bytes to an int? :confused:

Already thankful for any thoughts,
Pedro Doria Meunier

Lykurg
15th July 2009, 18:36
in251 holds the value: "J[²

This piece of code:


bool ok;
msgTime = in251.left(4).toUInt(&ok);
if (!ok) printf ("Conversion ERROR!\r\n" );


always returns "Conversion error" and msgTime, of course, is set to zero.

Why can't .toUint() convert those bytes to an int?

? Have you looked what in251.left(4) gives you back if the value of in251 is "J[²"? That can't work. Use QString::number() to convert a number in a string/byte array!

qWarning() << QString::number(input251).left(4).toInt();