Results 1 to 3 of 3

Thread: Reading an HEX file and store into a QByteArray

  1. #1
    Join Date
    Nov 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Reading an HEX file and store into a QByteArray

    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?


    Qt Code:
    1. QFile file(ui->lineEdit_FilePath->text().toLatin1());
    2.  
    3. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    4. return;
    5.  
    6. ui->lineEditSize->setText(QString::number(file.size()));
    7.  
    8. QTextStream fileTextStream(&file);
    9.  
    10. QByteArray contentHex = fileTextStream.readAll().toLocal8Bit());
    11.  
    12. ui->lineEdit_1->setText(contentHex.mid(0,4).toHex());
    To copy to clipboard, switch view to plain text mode 


    I also tried doing this:

    Qt Code:
    1. QString content = fileTextStream.readAll();
    2. QByteArray contentHex = content.toLatin1().toHex();
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading an HEX file and store into a QByteArray

    You have to use QFile not QTextStream.

  3. #3
    Join Date
    Nov 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading an HEX file and store into a QByteArray

    Thanks,

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

    Qt Code:
    1. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    2. return;
    To copy to clipboard, switch view to plain text mode 

    To this:

    Qt Code:
    1. if (!file.open(QIODevice::ReadOnly))
    2. return;
    To copy to clipboard, switch view to plain text mode 


    Thanks a lot!

Similar Threads

  1. reading wrong data from QByteArray
    By avanindra in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2013, 14:06
  2. qbytearray qdatastream qfile writing/reading
    By pekal in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2012, 22:51
  3. store number in QByteArray
    By sattu in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2011, 14:27
  4. Reading from QByteArray
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 17:17
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 21:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.