Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Binary files and conversion

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question Binary files and conversion

    I'd like to read binary files and generate an output consisting of normal 8-bit numerical values corresponding to the bytes.
    In other words, the input is a binary fine and output should be an array of values 0-255.

    1. How should the file be opened/read?
    2. Is there an automatic conversion from binary to 8-bit?

    Thank you!

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

    Default Re: Binary files and conversion

    What do you think is the difference between binary data and bytes with values ​​from 0-255 ?

  3. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Binary files and conversion

    Hi Lesiok,

    There is no difference, the data are always stored as binary. In the text format there are things like newline character etc. My question was:
    What would be the best way to get this conversion done and should that involve opening and reading the files in another way (such as using different file handling commands and classes) specific to Qt.

    Thanks.

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

    Default Re: Binary files and conversion

    What conversion ? You say : there is no difference. Can tell precisely what is binary file. Are this data formatted or this is a stream of bytes.
    P.S.
    You are asking about QDataStream or QFile
    Last edited by Lesiok; 23rd November 2012 at 14:27.

  5. #5
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Binary files and conversion

    Hi,

    What I mean is how to read it byte by byte and do the conversion so that the output is a text file with numbers between 0 and 255.

    Thanks

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

    Default Re: Binary files and conversion

    RTFM - read this funny manual
    To read data from file use QFile and to write this data to text file use QTextStream. And next time simple ask : how write data to text file.

  7. #7
    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: Binary files and conversion

    Quote Originally Posted by timmu View Post
    What I mean is how to read it byte by byte and do the conversion so that the output is a text file with numbers between 0 and 255.
    Just read from the input device using the QIODevice read API and use QString::number() on each byte, or read fixed sized blocks and create strings for each block by using QString::sprintf with as many arguments as you have bytes in your blocks.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    timmu (26th November 2012)

  9. #8
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Binary files and conversion

    I was wondering why the code below doesn't work. I'm trying to read a binary file byte by bite and convert it into text or numbers, preferably to numbers.

    Qt Code:
    1. char readChar;
    2.  
    3. QFile infile(Inname); if(!infile.open(QIODevice::ReadOnly)) {exit(1);}
    4. QTextStream in_stream(&infile);
    5.  
    6. QFile outfile(Outname); if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {exit(1);}
    7. QTextStream out_stream(&outfile);
    8.  
    9.  
    10. while(!in_stream.atEnd())
    11. {
    12. in_stream >> readChar;
    13. if(in_stream.atEnd()) {break;}
    14. out_stream << readChar << "\n";
    15. }
    To copy to clipboard, switch view to plain text mode 

    Then this should also allow reading binary file but how to convert it into int?:

    Qt Code:
    1. qint64 bufSize = 1024;
    2. char *buf = new char[bufSize];
    3. qint64 dataSize;
    4. while (!in_stream.atEnd())
    5. {
    6. dataSize = infile.read(buf, bufSize);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by timmu; 26th November 2012 at 14:01.

  10. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Binary files and conversion

    Because You are using variable of type char. So if in readChar You have value 65 decimal in text file You will get letter A because 65 is ASCII code of letter A. In first example change line 14 to :
    Qt Code:
    1. out_stream << (int)readChar << "\n";
    To copy to clipboard, switch view to plain text mode 
    This is abc of C/C++ language and have nothing to Qt.

  11. The following user says thank you to Lesiok for this useful post:

    timmu (26th November 2012)

  12. #10
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Binary files and conversion

    Thank you very much, this made it work. Now I have this problem:

    The following script only outputs the values whose 'signed 8-bit value' is positive. If it is negative, the corresponding value is not written into the output file. Why is that?

    Qt Code:
    1. char readChar;
    2.  
    3. QFile infile(Inname); if(!infile.open(QIODevice::ReadOnly)) {exit(1);}
    4. QTextStream in_stream(&infile);
    5.  
    6. QFile outfile(Outname); if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {exit(1);}
    7. QTextStream out_stream(&outfile);
    8.  
    9.  
    10. while(!in_stream.atEnd())
    11. {
    12. in_stream >> readChar;
    13. if(in_stream.atEnd()) {break;}
    14. out_stream << (int)readChar << "\n";
    15. }
    To copy to clipboard, switch view to plain text mode 

  13. #11
    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: Binary files and conversion

    Try
    Qt Code:
    1. out_stream << QString::number(static_cast<uchar>(readChar))
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  14. The following user says thank you to anda_skoa for this useful post:

    timmu (27th November 2012)

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Binary files and conversion

    If your file contains binary data then why are you reading it with QTextStream?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #13
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Binary files and conversion

    Hi Wysota,

    How would you recommend to read the file? The code change recommended above didn't solve the problem, I'm still not seeing all of the binary values converted to numbers.

    Thanks!
    Last edited by timmu; 27th November 2012 at 09:57.

  17. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Binary files and conversion

    Quote Originally Posted by timmu View Post
    How would you recommend to read the file?
    With QFile and its QIODevice API.

    What I understand is that you simply want a hex dump of the file. The most trivial approach (albeit far from optimal in terms of memory) is:

    Qt Code:
    1. QFile in("infile");
    2. QFile out("outfile");
    3. in.open(QIODevice::ReadOnly);
    4. out.open(QIODevice::WriteOnly);
    5. out.write(in.readAll().toHex());
    To copy to clipboard, switch view to plain text mode 

    You can do the same if you want a decimal encoding, only that you need to iterate over each byte in the array and convert it manually to a decimal value.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. The following user says thank you to wysota for this useful post:

    timmu (27th November 2012)

  19. #15
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Binary files and conversion

    Thanks Wysota, this is a very good idea. Indeed, I actually need decimal encoding. If I need to iterate over each byte to make the conversion, then I cannot use readAll(), is that correct? In that case I need to look at each byte individually somehow.

  20. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Binary files and conversion

    Quote Originally Posted by timmu View Post
    If I need to iterate over each byte to make the conversion, then I cannot use readAll(), is that correct?
    You can use readAll(). You can't use toHex().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #17
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Binary files and conversion

    I've been playing with the code below. I'd like to read all the bytes of the binary file "infile" one by one and output all the bits (in the form of 1/0) so that I get one number per line per line. Example:
    1
    0
    1
    1
    0
    1
    0
    0

    What is the best way to achieve that?

    Qt Code:
    1. QFile in("infile");
    2. QFile out("outfile");
    3. in.open(QIODevice::ReadOnly);
    4. out.open(QIODevice::WriteOnly);
    5. out.write(in.readAll());
    To copy to clipboard, switch view to plain text mode 

  22. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Binary files and conversion

    Quote Originally Posted by timmu View Post
    What is the best way to achieve that?
    The same as earlier. Read data and convert it to the notation of your choice. One byte is eight bits. For each byte extract each subsequent bit and print it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  23. #19
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Binary files and conversion

    Thanks.

    I guess my question was how to do it byte by byte.

  24. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Binary files and conversion

    Quote Originally Posted by timmu View Post
    I guess my question was how to do it byte by byte.
    Qt Code:
    1. while(!file.atEnd()) {
    2. char c;
    3. if(!file.readChar(&c)) break;
    4. print_bit_by_bit(c);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  25. The following user says thank you to wysota for this useful post:

    timmu (11th December 2012)

Similar Threads

  1. How to write and read from binary files
    By Momergil in forum Newbie
    Replies: 9
    Last Post: 28th November 2011, 12:58
  2. integrated + binary resource files
    By Le_B in forum Qt Quick
    Replies: 0
    Last Post: 5th June 2011, 17:45
  3. Reading Binary Files
    By archanasubodh in forum Newbie
    Replies: 1
    Last Post: 27th February 2008, 13:31
  4. How can I read Binary files with Qt
    By geo_saleh in forum Qt Programming
    Replies: 2
    Last Post: 16th August 2007, 11:37
  5. Concatenating two binary files
    By nbkhwjm in forum Newbie
    Replies: 13
    Last Post: 23rd April 2007, 04:30

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.