PDA

View Full Version : Reading an HEX file and store into a QByteArray



chs
9th May 2017, 16:47
Hello,

I have an issue when I try to read an HEX document and store the content into a QByteArray. What I want to get is a QByteArray with HEX content and show the content in a LineEdit.

I works for almost perfect but is not working when one byte is "0x0d" (\r). ¿Which is the correct conversion type when reading the file to an HEX QByteArray?





QFile file(ui->lineEdit_FilePath->text().toLatin1());

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

ui->lineEditSize->setText(QString::number(file.size()));

QTextStream fileTextStream(&file);

QByteArray contentHex = fileTextStream.readAll().toLocal8Bit());

ui->lineEdit_1->setText(contentHex.mid(0,4).toHex());





I also tried doing this:



QString content = fileTextStream.readAll();
QByteArray contentHex = content.toLatin1().toHex();


In this case I had a problem when reading values upper than 0x7f but I solved it using ".toLocal8Bit()" but now the problem is with "0x0d" byte values.



Thanks in advance.

Lesiok
10th May 2017, 07:16
You have to use QFile not QTextStream.

chs
10th May 2017, 07:45
Thanks,

I removed the QTextStream and also I had to change this line to work:



if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;


To this:



if (!file.open(QIODevice::ReadOnly))
return;



Thanks a lot!