Conversion from QByteArray to int
-
I tried something else that runs pretty well but in the wrong order:
info.toHex().toInt(&ok, 16);The result is 256.
It means that it has been read in the wrong order.
How can I specify that first byte is LSB and second one MSB ?@mulfycrowh said in Conversion from QByteArray to int:
info.toHex().toInt(&ok, 16);The intermediate conversion to hex is unnecessary.
The result is 256. It means that it has been read in the wrong order.
Whether that code will produce the right result or not, depends on the endianness of the host. In your case, as you said, its the wrong order. On another platform, it would be the correct order.
How can I specify that first byte is LSB and second one MSB
Exactly as I suggested above :)
#include <QtEndian> ... const int count = qFromLittleEndian<qint16>(info.data());Here the qFromLittleEndian function knows how to convert from little-endian (least significant byte first), to whatever endianness your host is (which may or may not be the same endianness).
Give it a go.
Cheers.
-
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.