A peculiar thing that I encountered is that in a loop that I am doing where I am populating a QByteArray
with a chunks of a QStringList
. To be more precise, the QStringList
takes a standard QString
and splits it into smaller bits on every time it encounters a "-" within the string. Ergo, if a QString
containing "A1-B2-C3-D4-E5"
, it would populate the list as small chunks (list[0]="A1",list[1]="B2",list[2]="C3",...
). However, I need those bytes to populate a QByteArray
and when I use a loop it only takes the chars of it and fills the QByteArray
as bytearray[0]="A",bytearray[1]="1",bytearray[2]="2"
and so on. Considering the code I am using, I am wondering what wrong has happened?
Here is the code:
QStringList inputArray = input.split('-');
QByteArray output;
for(int i = 0; i < inputArray.count(); i++)
{
output.append(inputArray.at(i).toLatin1());
}
ui->lineEdit->setText(output);
qDebug() << QByteArray("ACDC"); // outputs "ACDC"
Provided that those character pairs are indeed hexadecimals, you need to tell specify that:
qDebug() << QByteArray::fromHex("ACDC"); // outputs "\xAC\xDC"
The former byte array will be 4 bytes long, whereas the latter will only be 2 bytes long, as each char pair is decoded as hexadecimal.