Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

Thread: Binary files and conversion

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

    Default Re: Binary files and conversion

    Thanks Wysota!

    This following gives me all byte integer values but how do I get the individual bits out of it.
    What I need is a continuous array of 0's and 1's.


    Qt Code:
    1. void binarytobits()
    2. {
    3. int i, mybit;
    4.  
    5. QFile in("indata.dat");
    6.  
    7. if (!in.open(QIODevice::ReadOnly)) return;
    8.  
    9.  
    10. QByteArray blob = in.readAll();
    11. int size = blob.size();
    12.  
    13.  
    14. for(i=0; i<size; i++)
    15. {
    16. mybit=blob.at(i); cout << mybit << "\n";
    17. }
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    I wonder if this is a valid and good way to convert QByteArray to QBitArray

    Qt Code:
    1. QByteArray blob = in.readAll();
    2. QBitArray blob2=QVariant(blob2).toBitArray();
    To copy to clipboard, switch view to plain text mode 
    Last edited by timmu; 11th December 2012 at 09:36.

  2. #22
    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
    This following gives me all byte integer values but how do I get the individual bits out of it.
    Do you have ANY C/C++ knowledge? So far we are guiding you step by step and you don't seem to be making any effort yourself...

    Use bit shifting or masking to extract specific bits from a byte value. This is a strictly C/C++ task, please try to solve it yourself.
    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.


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

    Default Re: Binary files and conversion

    Thanks!

    Yes I do, I just don't know the bit operations. I was curious if Qt had something to that effect.

  4. #24
    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

    There is no need for Qt to have "something to that effect" if the programming language already provides it. There is no QAddTwoIntegers class in Qt the same way as there is no QInteger and no QIntegerAsBitString class. Even if there was no bit shifting in C, you should still be able to think your way through using regular integer divison.
    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.


  5. #25
    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 cannot read binary file and create a file with two columns:
    showing the numerical value of each byte and then also its binary representation?

    Qt Code:
    1. void binarytobits()
    2. {
    3. int i, size;
    4. char mybyte;
    5.  
    6. QFile in("infile.dat"); if (!in.open(QIODevice::ReadOnly)) return;
    7. QFile out("outfile.txt"); if (!out.open(QIODevice::WriteOnly)) return;
    8. QTextStream out_stream(&out);
    9.  
    10. QByteArray blob = in.readAll();
    11. size = blob.size();
    12.  
    13. for(i=0; i<size; i++)
    14. {
    15. mybyte=blob.at(i);
    16. QString myBits( QString::number( mybyte, sizeof(&mybyte) ) );
    17. out_stream << mybyte << " " << myBits <<"\n";
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. #26
    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

    Read the docs for QString::number().

    Hint: sizeof(&mybyte) = sizeof(char*) = 4 (or 8)
    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.


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

    timmu (11th December 2012)

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

    Default Re: Binary files and conversion

    Indeed sizeof(char*) is 4 in my case.

    Are you suggesting that QString::number() is not appropriate for what I'm doing?

    The code above is generating a binary file and I cannot open it with text editor. Why is it binary?

  9. #28
    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
    Are you suggesting that QString::number() is not appropriate for what I'm doing?
    I'm suggesting you should read the documentation.

    The code above is generating a binary file and I cannot open it with text editor. Why is it binary?
    No idea. Text files are also binary files.
    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.


Similar Threads

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