Writing both ASCII and Binary with QDataStream.



  • 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 :)



  • 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?


  • Moderators

    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.



  • 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.


  • Moderators

    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.



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



  • 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


  • Moderators

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



  • 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


  • Moderators

    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).



  • 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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.