Results 1 to 7 of 7

Thread: redaing binary file

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question redaing binary file

    I have a requirement where I have to ready a binary file and search for a particular address given by user and store the hex value of corresponding address in an array.
    I have a file like "39059921.HA". Which i have to read on a button click.
    I have opened the file in hex viewer and the inside content are like below
    Block 0 Strarts at:0x 0 Ends at: 0x4319 (Length:0x431A=17178)
    00000000: 03 01 00 02 04 00 55 50 41 00 00 00 00 00 00 00 ......UPA.....
    00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .................
    00000020: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF .................
    00000030: 66 65 74 79 00 00 00 00 00 00 00 00 CD 38 1A D1 .fety........
    -------
    -------
    I tried reading the file like normal text file but i am getting some junk value.
    Qt Code:
    1. QFile file("39059921.HA");
    2.  
    3. if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    4. {
    5. return;
    6. }
    7. QTextStream stream(&file);
    8. while(!stream.atEnd())
    9. {
    10. QString line = stream.readLine();
    11. qDebug()<<"data"<<line;
    12. }
    13.  
    14. file.close();
    15.  
    16. output:-I get the output like below
    17. data""—¶GÐEÐk|‘s‰žÃ*¥òm[ˆ8êܳ$7¢,HYNܾlFTÛ/õêÃâlÂ¥Bóîä1©»0*¢Ïƒ©è~Êßl«Ê2<ˆWXˆ³8{Èà IÃ…@"
    18. data"&ÓûcVp„êp1¡Çø–ڐñ T77”(/•´„ŸÎµ°©¯”9·|ÞmJ"5²ŒBÂ¡ó” ˜NWeB(¯e£÷•ˆ^Ó$Òo›ùt{€"“Ts²–)‡hexText "ppÀhexText "pqÆ pˆà p•ÂÐx‡õphà "
    To copy to clipboard, switch view to plain text mode 
    How can I read the above and search for a particular address

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: redaing binary file

    As you said, it is a binary file so don't treat it like a text file.

    Open it without the text flag, seek to the position, read as many bytes as you want using QIODevice::read() (which QFile is a subclass of).

    Cheers,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: redaing binary file

    Actually its a Hex file which contain address and 16 bit hex data like below
    00000020: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    I have tried reading as below
    Qt Code:
    1. QFile file("abcdef.dat");
    2.  
    3. if(!file.open(QIODevice::ReadOnly))
    4. {
    5. return;
    6. }
    7. QTextStream stream(&file);
    8. while(!stream.atEnd())
    9. {
    10. QString line = stream.readLine();
    11. QString hexText = QByteArray::fromHex(file.readLine());
    12. qDebug()<<"Text"<<line;
    13. qDebug()<<"hexText"<<hexText ;
    14. }
    15.  
    16. file.close();
    To copy to clipboard, switch view to plain text mode 
    but still not able to read.
    Please let me know can we parse or read the hex file as like other text file.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: redaing binary file

    Are you sure it is a text file that contains text that is hex codes?
    Can a text editor open it?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: redaing binary file

    The file I can open only with Hex view Tool.
    It can not be open with normal notepad or notepad++. If you try it will show some junk character
    Its not a text file.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: redaing binary file

    Then why do you keep treating it like a text file?

    If a text editor can't read it, why do you expect QTextStream to be able to?

    Are you aware that not all byte sequences can be interpreted as text?

    Cheers,
    _

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: redaing binary file

    There is a common inability to tell the difference between the human-readable representation of a set of bytes and the actual bytes at play here. This leads to the incorrect notion that the hexadecimal representation == the actual bytes. Once your brain is there it seems logical that fromHex() would be needed.

    As you have already pointed out, this is what the OP wants to read a few bytes from the middle of the file:
    Qt Code:
    1. QFile file("abcdef.dat");
    2. if(file.open(QIODevice::ReadOnly)) {
    3. if (file.seek(100)) {
    4. QByteArray bytes = file.read(6);
    5. // do something with bytes
    6. }
    7. file.close();
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. plotting a binary file
    By Alex22 in forum Newbie
    Replies: 1
    Last Post: 7th November 2015, 20:41
  2. File Editing (Binary/Hex) with Qt 4.6.2
    By RH-00 in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2010, 20:58
  3. Open a binary file.
    By TJSonic in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2010, 22:32
  4. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 17:04
  5. How to Print a doc file (or binary file) to printer
    By rmagro in forum Qt Programming
    Replies: 15
    Last Post: 5th September 2008, 15:46

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
  •  
Qt is a trademark of The Qt Company.