Conversion from QByteArray to int
-
I tried what you suggested:
qFromLittleEndian<quint16>(info.data());I've got the compiler error :
C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types
-
I mean qint16.
Same issue -
I tried what you suggested:
qFromLittleEndian<quint16>(info.data());I've got the compiler error :
C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types
@mulfycrowh said in Conversion from QByteArray to int:
I've got the compiler error :
C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument typesAh... from the qFromLittleEndian docs:
Note: Since Qt 5.7, the type of the src parameter is a void pointer.
For pre-5.7, you will need to cast to
uchar, like:int count = qFromLittleEndian<qint16>(reinterpret_cast<const uchar *>(info.data()));(I just tested that on Qt 5.5.1)
Cheers.
-
thanks, it runs perfectly !
-
thanks, it runs perfectly !
@mulfycrowh
A two byte sequence is ashort, notint. You should use the data stream classes for such deserializations instead of going to type-safety hell:QByteArray data; //< This is your data QDataStream in(data); //< Attach a read-only stream to it in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order qint16 result; //< The result you want in >> result; //< Just read it from the stream -
@mulfycrowh
A two byte sequence is ashort, notint. You should use the data stream classes for such deserializations instead of going to type-safety hell:QByteArray data; //< This is your data QDataStream in(data); //< Attach a read-only stream to it in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order qint16 result; //< The result you want in >> result; //< Just read it from the stream@kshegunov said in Conversion from QByteArray to int:
QByteArray data; //< This is your data
QDataStream in(data); //< Attach a read-only stream to it
in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte orderqint16 result; //< The result you want
in >> result; //< Just read it from the streamHi i want to read more than one number and i can read only first number how can i read all of them plese help me :) !!
-
@kshegunov said in Conversion from QByteArray to int:
QByteArray data; //< This is your data
QDataStream in(data); //< Attach a read-only stream to it
in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte orderqint16 result; //< The result you want
in >> result; //< Just read it from the streamHi i want to read more than one number and i can read only first number how can i read all of them plese help me :) !!
-
@sude
It does work. So your code is wrong, or your data is not what you expect, or you are not looking at the right thing. Suggest you open your own new topic for this and post your problem/code; this topic is very old.