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
    4 Apr 2018, 20:38

    @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);
     }
    
    H Offline
    H Offline
    Hollywood33
    wrote on 5 Apr 2018, 13:18 last edited by
    #25

    @Cobra91151 can't build: nullptr is not declared in this scope;
    tobitstring is not declared in this scope

    M C 2 Replies Last reply 5 Apr 2018, 15:04
    0
    • H Hollywood33
      5 Apr 2018, 13:18

      @Cobra91151 can't build: nullptr is not declared in this scope;
      tobitstring is not declared in this scope

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Apr 2018, 15:04 last edited by
      #26

      @Hollywood33
      odd it dont know nullptr but you can use 0 (zero) or NULL instead
      also just remove
      myTextStream << toBitString(QString("This is a test!"));
      --->
      myTextStream << QString("This is a test!");

      C H 2 Replies Last reply 5 Apr 2018, 15:57
      1
      • M mrjj
        5 Apr 2018, 15:04

        @Hollywood33
        odd it dont know nullptr but you can use 0 (zero) or NULL instead
        also just remove
        myTextStream << toBitString(QString("This is a test!"));
        --->
        myTextStream << QString("This is a test!");

        C Offline
        C Offline
        Cobra91151
        wrote on 5 Apr 2018, 15:57 last edited by
        #27

        @mrjj

        The code - myTextStream << QString("This is a test!"); will write only text not bit representation as he wants. So it must be - myTextStream << toBitString(QString("This is a test!"));

        1 Reply Last reply
        2
        • H Hollywood33
          5 Apr 2018, 13:18

          @Cobra91151 can't build: nullptr is not declared in this scope;
          tobitstring is not declared in this scope

          C Offline
          C Offline
          Cobra91151
          wrote on 5 Apr 2018, 15:59 last edited by Cobra91151 4 May 2018, 16:02
          #28

          @Hollywood33

          You must declare tobitstring function in the header (.h file) to use it. + Specify more info about your building process (Qt version, compiler, OS)?

          Also, instead of nullptr, you can use - Q_NULLPTR macro or add the QString *selectedFilter as the parameter points to in the docs - QFileDialog.

          H 1 Reply Last reply 5 Apr 2018, 16:36
          0
          • M mrjj
            5 Apr 2018, 15:04

            @Hollywood33
            odd it dont know nullptr but you can use 0 (zero) or NULL instead
            also just remove
            myTextStream << toBitString(QString("This is a test!"));
            --->
            myTextStream << QString("This is a test!");

            H Offline
            H Offline
            Hollywood33
            wrote on 5 Apr 2018, 16:03 last edited by
            #29

            @mrjj it writes test text, no 0&1s.

            I was told to read into QByteArray and then send it to stream:
            QFile f("/Users/ro888.jpg");
            f.open(QIODevice::ReadOnly);
            QByteArray ba = f.readAll();
            stream << ba;

            So I have to update code in the following example using QByteArray:
            How to do it?

            void MainWindow::on_pushButton_3_clicked()
            {
            QFile file("/Users/file.bin");
            if(file.open(QIODevice::WriteOnly))
            {
            QDataStream stream(&file);
            stream. setVersion(QDataStream::Qt_4_2);
            stream << QPointF(30, 30) << QImage("/Users/ro888.jpg");
            if(stream.status() != QDataStream::Ok)
            {
            qDebug() << "Error";
            }
            }
            file.close();
            }

            M 2 Replies Last reply 5 Apr 2018, 16:05
            0
            • H Hollywood33
              5 Apr 2018, 16:03

              @mrjj it writes test text, no 0&1s.

              I was told to read into QByteArray and then send it to stream:
              QFile f("/Users/ro888.jpg");
              f.open(QIODevice::ReadOnly);
              QByteArray ba = f.readAll();
              stream << ba;

              So I have to update code in the following example using QByteArray:
              How to do it?

              void MainWindow::on_pushButton_3_clicked()
              {
              QFile file("/Users/file.bin");
              if(file.open(QIODevice::WriteOnly))
              {
              QDataStream stream(&file);
              stream. setVersion(QDataStream::Qt_4_2);
              stream << QPointF(30, 30) << QImage("/Users/ro888.jpg");
              if(stream.status() != QDataStream::Ok)
              {
              qDebug() << "Error";
              }
              }
              file.close();
              }

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 5 Apr 2018, 16:05 last edited by
              #30

              @Hollywood33
              Hi
              Yes sorry my bad.
              I missed that QString toBitString(const QVariant &v) was the function @J-Hilk provided :)
              So place
              QString toBitString(const QVariant &v) in .h and
              the body in the cpp as @Cobra91151 says :)

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

                @mrjj it writes test text, no 0&1s.

                I was told to read into QByteArray and then send it to stream:
                QFile f("/Users/ro888.jpg");
                f.open(QIODevice::ReadOnly);
                QByteArray ba = f.readAll();
                stream << ba;

                So I have to update code in the following example using QByteArray:
                How to do it?

                void MainWindow::on_pushButton_3_clicked()
                {
                QFile file("/Users/file.bin");
                if(file.open(QIODevice::WriteOnly))
                {
                QDataStream stream(&file);
                stream. setVersion(QDataStream::Qt_4_2);
                stream << QPointF(30, 30) << QImage("/Users/ro888.jpg");
                if(stream.status() != QDataStream::Ok)
                {
                qDebug() << "Error";
                }
                }
                file.close();
                }

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 5 Apr 2018, 16:09 last edited by
                #31

                @Hollywood33
                Hi
                Yes using QDataStream will be a binary file.
                You paths for the JPG files looks a bit odd but if you are sure they are valid then
                should produce a truly binary file :)

                1 Reply Last reply
                0
                • C Cobra91151
                  5 Apr 2018, 15:59

                  @Hollywood33

                  You must declare tobitstring function in the header (.h file) to use it. + Specify more info about your building process (Qt version, compiler, OS)?

                  Also, instead of nullptr, you can use - Q_NULLPTR macro or add the QString *selectedFilter as the parameter points to in the docs - QFileDialog.

                  H Offline
                  H Offline
                  Hollywood33
                  wrote on 5 Apr 2018, 16:36 last edited by
                  #32

                  @Cobra91151 I'm on Qt 5.1.0, OS X 10.6.8
                  I added to header #include "tobitstring.h" - error: no such file or directory

                  C 1 Reply Last reply 5 Apr 2018, 16:43
                  0
                  • H Hollywood33
                    5 Apr 2018, 16:36

                    @Cobra91151 I'm on Qt 5.1.0, OS X 10.6.8
                    I added to header #include "tobitstring.h" - error: no such file or directory

                    C Offline
                    C Offline
                    Cobra91151
                    wrote on 5 Apr 2018, 16:43 last edited by
                    #33

                    @Hollywood33 said in Encode/decode to binary file:

                    @Cobra91151 I'm on Qt 5.1.0, OS X 10.6.8
                    I added to header #include "tobitstring.h" - error: no such file or directory

                    You should add the prototype of the function to header file, not include it. Include works for additional files, not for functions!

                    For example:

                    test.h file:

                    public:
                        explicit Test(QWidget *parent = 0);
                        QString toBitString(const QVariant &v);
                        ~Test();
                    

                    It contains a constructor, prototype function and destructor.
                    And then put the body of the function in test.cpp file like this:

                    QString Test::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;
                    }
                    

                    Your class is - MainWindow, Test is just for testing purposes.

                    J 1 Reply Last reply 5 Apr 2018, 16:54
                    3
                    • C Cobra91151
                      5 Apr 2018, 16:43

                      @Hollywood33 said in Encode/decode to binary file:

                      @Cobra91151 I'm on Qt 5.1.0, OS X 10.6.8
                      I added to header #include "tobitstring.h" - error: no such file or directory

                      You should add the prototype of the function to header file, not include it. Include works for additional files, not for functions!

                      For example:

                      test.h file:

                      public:
                          explicit Test(QWidget *parent = 0);
                          QString toBitString(const QVariant &v);
                          ~Test();
                      

                      It contains a constructor, prototype function and destructor.
                      And then put the body of the function in test.cpp file like this:

                      QString Test::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;
                      }
                      

                      Your class is - MainWindow, Test is just for testing purposes.

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 5 Apr 2018, 16:54 last edited by
                      #34

                      @Cobra91151 I have to compliment your patience. *Thumbs up*


                      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.

                      1 Reply Last reply
                      3
                      • V Offline
                        V Offline
                        VRonin
                        wrote on 5 Apr 2018, 17:03 last edited by VRonin 4 May 2018, 20:11
                        #35

                        You can simplify it:

                        QString Test::toBitString(const QVariant &v)
                        {
                            const QByteArray bytes = v.toByteArray();
                            QString bitString;
                        for(const char* singlebyte = bytes.begin();singlebyte != bytes.end();++singlebyte)
                        bitString.append(QString::number(*singlebyte,2));
                            return bitString;
                        }
                        

                        But still looks absolutely useless to me

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        5

                        34/35

                        5 Apr 2018, 16:54

                        • Login

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