Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encode/decode to binary file
Forum Updated to NodeBB v4.3 + New Features

Encode/decode to binary file

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 7 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Cobra91151
    3 Apr 2018, 15:47

    @Hollywood33

    Hi! You can create a binary file with QDataStream class.

    H Offline
    H Offline
    Hollywood33
    wrote on 3 Apr 2018, 16:05 last edited by
    #3

    @Cobra91151 Could you please write in detail:
    void MainWindow::on_pushButton_3_clicked()
    {

    }

    A C 3 Replies Last reply 3 Apr 2018, 16:35
    0
    • H Hollywood33
      3 Apr 2018, 16:05

      @Cobra91151 Could you please write in detail:
      void MainWindow::on_pushButton_3_clicked()
      {

      }

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 3 Apr 2018, 16:35 last edited by
      #4

      @Hollywood33 do you really expect us to do your homework?

      you have nothing except some auto generated code. even the project description is a bit vague.

      you should refine your task (break it into baby steps) and then the implementation will become clearer.

      and be aware that you will need some C++ knowledge to perform your task.

      Qt has to stay free or it will die.

      1 Reply Last reply
      8
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 3 Apr 2018, 16:38 last edited by mrjj 4 Mar 2018, 16:45
        #5

        Hi
        A good start is the documentation
        http://doc.qt.io/qt-5/qdatastream.html
        it also contains samples and explain what is going on.
        as @Cobra91151 already mentions :)

        1 Reply Last reply
        5
        • H Hollywood33
          3 Apr 2018, 16:05

          @Cobra91151 Could you please write in detail:
          void MainWindow::on_pushButton_3_clicked()
          {

          }

          C Offline
          C Offline
          Cobra91151
          wrote on 3 Apr 2018, 16:48 last edited by
          #6

          @Hollywood33

          Ok. I will write some stuff a little bit later. I need to fix my ftp issue first.

          1 Reply Last reply
          0
          • H Hollywood33
            3 Apr 2018, 16:05

            @Cobra91151 Could you please write in detail:
            void MainWindow::on_pushButton_3_clicked()
            {

            }

            C Offline
            C Offline
            Cobra91151
            wrote on 3 Apr 2018, 17:21 last edited by
            #7

            @Hollywood33

            So, here is the code example to create/write data to a binary file:

            #include <QFile>
            #include <QDataStream>
            #include <QMessageBox>
            
            void MainWindow::on_pushButton_3_clicked()
            {
                    QFile *myBinaryFile = new QFile(qApp->applicationDirPath() + "/myDataFile.dat");
                    myBinaryFile->open(QIODevice::WriteOnly);
                    QDataStream binaryDataStream(myBinaryFile);
                    binaryDataStream.setVersion(QDataStream::Qt_5_9); //set QDataStream version for your Qt version if you need both forward and backward compatibility
                    binaryDataStream << QString("This is a test...");
                    myBinaryFile->close();
                    myBinaryFile->deleteLater();
                    QMessageBox::information(this, "Information", "Binary file created!", QMessageBox::Ok);
            }
            

            It writes a myDataFile.dat binary file to the application directory. If you want to read the data from the binary file, take a look at the documentation, the process will be similar. Happy coding!

            H 2 Replies Last reply 4 Apr 2018, 04:35
            3
            • C Cobra91151
              3 Apr 2018, 17:21

              @Hollywood33

              So, here is the code example to create/write data to a binary file:

              #include <QFile>
              #include <QDataStream>
              #include <QMessageBox>
              
              void MainWindow::on_pushButton_3_clicked()
              {
                      QFile *myBinaryFile = new QFile(qApp->applicationDirPath() + "/myDataFile.dat");
                      myBinaryFile->open(QIODevice::WriteOnly);
                      QDataStream binaryDataStream(myBinaryFile);
                      binaryDataStream.setVersion(QDataStream::Qt_5_9); //set QDataStream version for your Qt version if you need both forward and backward compatibility
                      binaryDataStream << QString("This is a test...");
                      myBinaryFile->close();
                      myBinaryFile->deleteLater();
                      QMessageBox::information(this, "Information", "Binary file created!", QMessageBox::Ok);
              }
              

              It writes a myDataFile.dat binary file to the application directory. If you want to read the data from the binary file, take a look at the documentation, the process will be similar. Happy coding!

              H Offline
              H Offline
              Hollywood33
              wrote on 4 Apr 2018, 04:35 last edited by
              #8

              @Cobra91151 Thank you very much!

              1 Reply Last reply
              0
              • C Cobra91151
                3 Apr 2018, 17:21

                @Hollywood33

                So, here is the code example to create/write data to a binary file:

                #include <QFile>
                #include <QDataStream>
                #include <QMessageBox>
                
                void MainWindow::on_pushButton_3_clicked()
                {
                        QFile *myBinaryFile = new QFile(qApp->applicationDirPath() + "/myDataFile.dat");
                        myBinaryFile->open(QIODevice::WriteOnly);
                        QDataStream binaryDataStream(myBinaryFile);
                        binaryDataStream.setVersion(QDataStream::Qt_5_9); //set QDataStream version for your Qt version if you need both forward and backward compatibility
                        binaryDataStream << QString("This is a test...");
                        myBinaryFile->close();
                        myBinaryFile->deleteLater();
                        QMessageBox::information(this, "Information", "Binary file created!", QMessageBox::Ok);
                }
                

                It writes a myDataFile.dat binary file to the application directory. If you want to read the data from the binary file, take a look at the documentation, the process will be similar. Happy coding!

                H Offline
                H Offline
                Hollywood33
                wrote on 4 Apr 2018, 09:02 last edited by
                #9

                @Cobra91151 Hi! I tried, file created but it's not binary, just plain test text in in.
                I want it to look like this: 01101001010101010010.

                J 1 Reply Last reply 4 Apr 2018, 09:11
                0
                • H Hollywood33
                  4 Apr 2018, 09:02

                  @Cobra91151 Hi! I tried, file created but it's not binary, just plain test text in in.
                  I want it to look like this: 01101001010101010010.

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 4 Apr 2018, 09:11 last edited by
                  #10

                  @Hollywood33 how do you think data is stored on your HD?

                  You happen to have a good text-editor that interpretes the 01's as ASCII-chars and displays them.

                  Not a problem of Qt but your external file-viewer.

                  I think what you want is to save Strings/Numbers as human readable 0/1 in a *.txt file. Correct?
                  Than you'll have to create a function that takes a String/Number and returns a fromated string, that you than can save into your file.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  H 1 Reply Last reply 4 Apr 2018, 09:39
                  3
                  • J J.Hilk
                    4 Apr 2018, 09:11

                    @Hollywood33 how do you think data is stored on your HD?

                    You happen to have a good text-editor that interpretes the 01's as ASCII-chars and displays them.

                    Not a problem of Qt but your external file-viewer.

                    I think what you want is to save Strings/Numbers as human readable 0/1 in a *.txt file. Correct?
                    Than you'll have to create a function that takes a String/Number and returns a fromated string, that you than can save into your file.

                    H Offline
                    H Offline
                    Hollywood33
                    wrote on 4 Apr 2018, 09:39 last edited by
                    #11

                    @J.Hilk I opened it in TextEdit on mac, notepad in Win. You are right, I need sequence of "0" and "1" in .txt file. Please write example using QFileDialog.
                    It has to work with all file extensions and file sizes.

                    J 1 Reply Last reply 4 Apr 2018, 10:34
                    0
                    • H Hollywood33
                      4 Apr 2018, 09:39

                      @J.Hilk I opened it in TextEdit on mac, notepad in Win. You are right, I need sequence of "0" and "1" in .txt file. Please write example using QFileDialog.
                      It has to work with all file extensions and file sizes.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 4 Apr 2018, 10:34 last edited by jsulm 4 Apr 2018, 10:34
                      #12

                      @Hollywood33 said in Encode/decode to binary file:

                      It has to work with all file extensions and file sizes.

                      What does it have to do with file extension and file size?!
                      If I understood you correctly all you want to do is writing a text file containing 0 and 1 as characters, right?
                      File extension is simply part of file name and does not define its format. You could store a picture as "mypicture.xml" - it still would be a picture and not XML.
                      If you would have checked the documentation (http://doc.qt.io/qt-5/qfile.html) you would have found this:

                      QFile file("out.txt");
                      if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                          return;
                      
                      QTextStream out(&file);
                      out << "The magic number is: " << 49 << "\n";
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply 4 Apr 2018, 11:11
                      3
                      • M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 4 Apr 2018, 10:37 last edited by
                        #13

                        Hi
                        I read it as he just want a binary file but do not understand that
                        some editors will not show the raw binary but show
                        it translated. So not sure he wants ASCII 0 and 1 but meant
                        it as a way to say binary which QDataStream already is :)

                        J 1 Reply Last reply 4 Apr 2018, 10:39
                        4
                        • M mrjj
                          4 Apr 2018, 10:37

                          Hi
                          I read it as he just want a binary file but do not understand that
                          some editors will not show the raw binary but show
                          it translated. So not sure he wants ASCII 0 and 1 but meant
                          it as a way to say binary which QDataStream already is :)

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 4 Apr 2018, 10:39 last edited by
                          #14

                          @mrjj @Hollywood33 OK, then I would suggest to use a hex-editor instead of a text editor :-)

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          3
                          • J jsulm
                            4 Apr 2018, 10:34

                            @Hollywood33 said in Encode/decode to binary file:

                            It has to work with all file extensions and file sizes.

                            What does it have to do with file extension and file size?!
                            If I understood you correctly all you want to do is writing a text file containing 0 and 1 as characters, right?
                            File extension is simply part of file name and does not define its format. You could store a picture as "mypicture.xml" - it still would be a picture and not XML.
                            If you would have checked the documentation (http://doc.qt.io/qt-5/qfile.html) you would have found this:

                            QFile file("out.txt");
                            if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                                return;
                            
                            QTextStream out(&file);
                            out << "The magic number is: " << 49 << "\n";
                            
                            H Offline
                            H Offline
                            Hollywood33
                            wrote on 4 Apr 2018, 11:11 last edited by
                            #15

                            @jsulm it writes plain text.
                            I want file to be saved in .txt file as sequence of 0 and 1. It must look like 01010001 in simple text editor, like Notepad, not HEX viewer.

                            J 1 Reply Last reply 4 Apr 2018, 11:16
                            0
                            • H Hollywood33
                              4 Apr 2018, 11:11

                              @jsulm it writes plain text.
                              I want file to be saved in .txt file as sequence of 0 and 1. It must look like 01010001 in simple text editor, like Notepad, not HEX viewer.

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 4 Apr 2018, 11:16 last edited by
                              #16

                              @Hollywood33 Then you want to write text. Because text editors (like the name suggests) only understand text. It wouldn't be a binary file. You would write 1 and 0 as characters:

                              QFile file("out.txt");
                              if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                                  return;
                              
                              QTextStream out(&file);
                              out << "010001100101110";
                              

                              You need to understand the difference between text and binary. 0 as character in a text file is stored as ASCII number 48, 1 is 49. That means if you store 01 in a text file and open it in a hex editor you will see 4849 (or 0x30 0x31 as hex).

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              H 1 Reply Last reply 4 Apr 2018, 11:32
                              1
                              • J jsulm
                                4 Apr 2018, 11:16

                                @Hollywood33 Then you want to write text. Because text editors (like the name suggests) only understand text. It wouldn't be a binary file. You would write 1 and 0 as characters:

                                QFile file("out.txt");
                                if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                                    return;
                                
                                QTextStream out(&file);
                                out << "010001100101110";
                                

                                You need to understand the difference between text and binary. 0 as character in a text file is stored as ASCII number 48, 1 is 49. That means if you store 01 in a text file and open it in a hex editor you will see 4849 (or 0x30 0x31 as hex).

                                H Offline
                                H Offline
                                Hollywood33
                                wrote on 4 Apr 2018, 11:32 last edited by
                                #17

                                @jsulm i know the difference between text and binary code.
                                I want to see 0&1's as UTF-8 text in *.txt file. What you have written is only writes text into file.

                                J 1 Reply Last reply 4 Apr 2018, 11:35
                                -1
                                • H Hollywood33
                                  4 Apr 2018, 11:32

                                  @jsulm i know the difference between text and binary code.
                                  I want to see 0&1's as UTF-8 text in *.txt file. What you have written is only writes text into file.

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 4 Apr 2018, 11:35 last edited by
                                  #18

                                  @Hollywood33 '1' and '0' are same in ASCII and UTF-8 as far as I know...

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    J.Hilk
                                    Moderators
                                    wrote on 4 Apr 2018, 11:37 last edited by J.Hilk 4 Apr 2018, 12:34
                                    #19

                                    because I'm a good guy :-)

                                    QString toBitString(const QVariant &v)
                                    {
                                        QByteArray bytes = v.toByteArray();
                                        QString bitString;
                                    
                                        QChar Zero('0');
                                        QChar One('1');
                                    
                                        for(int y(0); y <bytes.size(); y++){
                                            for(int i(0); i < 8; i++){
                                                bool b = (bytes[y] >> i) & 1;
                                                bitString.append(b ? One:Zero);
                                            }
                                        }
                                        return bitString;
                                    }
                                    

                                    Edit:
                                    Small edit in the code example, v.toByteArray().data() totally unnecessary!


                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    H 1 Reply Last reply 4 Apr 2018, 12:28
                                    2
                                    • J J.Hilk
                                      4 Apr 2018, 11:37

                                      because I'm a good guy :-)

                                      QString toBitString(const QVariant &v)
                                      {
                                          QByteArray bytes = v.toByteArray();
                                          QString bitString;
                                      
                                          QChar Zero('0');
                                          QChar One('1');
                                      
                                          for(int y(0); y <bytes.size(); y++){
                                              for(int i(0); i < 8; i++){
                                                  bool b = (bytes[y] >> i) & 1;
                                                  bitString.append(b ? One:Zero);
                                              }
                                          }
                                          return bitString;
                                      }
                                      

                                      Edit:
                                      Small edit in the code example, v.toByteArray().data() totally unnecessary!

                                      H Offline
                                      H Offline
                                      Hollywood33
                                      wrote on 4 Apr 2018, 12:28 last edited by
                                      #20

                                      @J.Hilk It looks like what I need. It remains only to understand how to use it with QFileDialog.

                                      M C 2 Replies Last reply 4 Apr 2018, 13:18
                                      0
                                      • H Hollywood33
                                        4 Apr 2018, 12:28

                                        @J.Hilk It looks like what I need. It remains only to understand how to use it with QFileDialog.

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 4 Apr 2018, 13:18 last edited by
                                        #21

                                        @Hollywood33
                                        Hi
                                        The docs shows how to use it.
                                        http://doc.qt.io/qt-5/qfiledialog.html
                                        It has sample :)

                                        1 Reply Last reply
                                        0
                                        • H Hollywood33
                                          4 Apr 2018, 12:28

                                          @J.Hilk It looks like what I need. It remains only to understand how to use it with QFileDialog.

                                          C Offline
                                          C Offline
                                          Cobra91151
                                          wrote on 4 Apr 2018, 20:38 last edited by
                                          #22

                                          @Hollywood33

                                          Ok, here is the code how I would save it with QFileDialog class using @J-Hilk solution.

                                          QString filters = "Text Documents (*.txt);;All files (*.*)";
                                          QString filePath = QFileDialog::getSaveFileName(this, "Save binary file", qApp->applicationDirPath() + "/MyFile", filters, nullptr);
                                          
                                           if (!filePath.isEmpty()) {
                                                QFile *myFile = new QFile(filePath);
                                                myFile->open(QIODevice::WriteOnly);
                                                QTextStream myTextStream(myFile); //QTextStream is used because you want to save it in .txt file
                                                myTextStream << toBitString(QString("This is a test!"));
                                                myFile->close();
                                                myFile->deleteLater();
                                                QMessageBox::information(this, "Information", "Binary file is saved!", QMessageBox::Ok);
                                           }
                                          
                                          M H 2 Replies Last reply 4 Apr 2018, 20:44
                                          2

                                          12/35

                                          4 Apr 2018, 10:34

                                          23 unread
                                          • Login

                                          • Login or register to search.
                                          12 out of 35
                                          • First post
                                            12/35
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved