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. Writing both ASCII and Binary with QDataStream.

Writing both ASCII and Binary with QDataStream.

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 7.3k 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.
  • M Offline
    M Offline
    mostafa27
    wrote on last edited by
    #1

    Hey, I'm having this problem of needing to write an ASCII header to a file, followed by Binary data.
    There was the ::setPrintableData(bool) that could do that, but it was removed from Qt4.
    How can I do that? I need the output to be something like this:

    @
    $$HEADERSTART
    $$BINARY
    $$UNITS/1
    $$VERSION/200
    $$DATE/170713
    $$HEADEREND
    PÖ PÃPà Pà Pà PÖ € Ä; PÃ,PÃPà Pà Pà PÃ,€ Z< PÃÂPÃPà Pà Pà PÀ ð< PÃXPÃPà Pà Pà PÃX€ †= PÃîPÃPà Pà Pà PÃî€ > PÄPÃPà Pà Pà PÄ€ ²> PÃPÃPà Pà Pà PÀ H? PðPÃPà Pà Pà Pð€ Þ? PÃFPÃPà Pà Pà PÃF€ t@ PÃÜPÃPà PÃ
    @
    Help would be incredibly appreciated :)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostafa27
      wrote on last edited by
      #2

      I tried using both QDataStream and QTextStream together but this is the result:
      @
      -zõ Ò¶’õ +CÎÂõ$$HEADERSTART
      $$BINARY
      $$UNITS/1
      $$VERSION/200
      $$DATE170713
      $$HEADEREND
      @

      Any ideas?

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        If you're not depend on "QTextStream's capabilities":http://qt-project.org/doc/qt-5.0/qtcore/qtextstream.html#details you are fine by using QDataStream only. Most Qt classes "serialize":http://qt-project.org/doc/qt-5.0/qtcore/datastreamformat.html well with QDataStream.

        Please show some code where you have problems.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mostafa27
          wrote on last edited by
          #4

          It's quite a big class but here's the part with the problems
          @
          QTextStream asc(this);

          //Header
          asc << "$$HEADERSTART\n";
          asc << "$$BINARY\n";
          asc << "$$UNITS/1\n";
          asc << "$$VERSION/200\n";
          asc << "$$DATE/";
          asc << time << "\n";
          asc << "$$HEADEREND\n";
          //Header end

          //Bin
          QDataStream bin (this);
          bin << 112125115125;
          bin << 12125115125;
          bin << 12112511312315125;
          //Bin end

          close();

          @

          I can't post more than this, but that's where the problem is occurring anyway. Number's are just random.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            and whats the type of "this"? I guess it's QFile?

            What about just something like this?
            @
            QDataStream stream(this);
            //headers
            stream << QString("$$HEADERSTART") << "\n";
            stream << QString("$$BINARY") << "\n";
            stream << QString("$$UNITS/1") << "\n";
            stream << QString("$$VERSION/200") << "\n";
            stream << QString("$$DATE/");
            stream << QString(time) << "\n";
            stream << QString("$$HEADEREND") << "\n";
            //Binary
            stream << QByteArray(binData);
            @

            If you need really ASCII try QString::toAscii() / QString::toLocal8Bit() conversion methods.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mostafa27
              wrote on last edited by
              #6

              Yea it's a QFIle.
              I'll give both your methods a shot. Thanks for your help!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mostafa27
                wrote on last edited by
                #7

                okay so the first option gets me
                @
                $ $ H E A D E R S T A R T
                $ $ B I N A R Y
                $ $ U N I T S / 1
                $ $ V E R S I O N / 2 0 0
                $ $ D A T E / šÙ
                $ $ L A Y E R S / C
                $ $ H E A D E R E N D
                Ò
                @

                I've tried checking the docs for info about toAscii, but I can't figure out how it works

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  whats wrong with this output? how does it differ from what you expect.
                  Show the code you used, maybe you misused it somehow ;)

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mostafa27
                    wrote on last edited by
                    #9

                    There are spaces between the characters, and binary stuff all around it that isn't showing on here.
                    No I used it correctly don't worry :D
                    I need it the Header part of the output to be real ASCII and the rest Binary. Like the example I posted in the first post/
                    Using both QdataStream and QTextStream together was the closest thing to what I wanted, but they were mixed up for some reason.
                    Thanks again man :D

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      ok..now i got ya.
                      The problem of using QDataStream is that it adds additional info for deserialization. In your case it's better to use QFile::write() directly.
                      Of if you still want to use streams you can do this:
                      @
                      QByteArray headerData;

                      QTextStream tStream(&headerData);
                      tStream << QString("$$HEADERSTART\n").toLocal8Bit();
                      tStream << QString("$$BINARY\n").toLocal8Bit();
                      tStream << QString("$$UNITS/1\n").toLocal8Bit();
                      tStream << QString("$$VERSION/200\n").toLocal8Bit();
                      tStream << QString("$$DATE/").toLocal8Bit();
                      tStream << QString("time\n").toLocal8Bit();
                      tStream << QString("$$HEADEREND\n").toLocal8Bit();
                      tStream.flush();

                          file.write( headerData );
                          file.write( binaryData );  //probably QByteArray??
                      

                      @
                      i guess you want to process your file in a non-Qt application later on? Otherwise it would be better to use the QDataStream approach because it's much easier to deserialize (if you have to).

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mostafa27
                        wrote on last edited by
                        #11

                        Thanks, you're a life saver!
                        Yea, it'll be read from non-Qt apps. I'm an intern helping with this project, so Qt is pretty new to me.
                        Thanks again :)

                        1 Reply Last reply
                        0

                        • Login

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