This would not work as expected because your input is a string (your example "00ff22ff55001188") containing 16 characters (
QChar
). If you need the bytes represented by two characters, you must get a character pair and convert it to a byte or just convert the whole string to a 64-bit value and split that into bytes.
A possible solution would be (assuming
DeviceMsg
is a
QString
with 16 characaters representing a 64-bit hex value):
QByteArray qba;
qba.resize(8);
qulonglong val = DeviceMsg.toULongLong(NULL, 16);
for (int i = 0; i < 8; i++)
{
qba[i] = static_cast<char>(val & 0xFF);
val >>= 8;
}
[EDIT]
According to the new information about DeviceMsg (see below comment) there is no need to use
QChar
and the solution is quite simple (see
http://doc.qt.io/qt-5/qbytearray.html#QByteArray-1[
^]):
QByteArray qba((const char *)DeviceMsg.DATA, sizeof(DeviceMsg.DATA));