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:
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
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
asc << "$$HEADERSTART\n";
asc << "$$BINARY\n";
asc << "$$UNITS/1\n";
asc << "$$VERSION/200\n";
asc << "$$DATE/";
asc << time << "\n";
asc << "$$HEADEREND\n";
QDataStream bin (this);
bin << 112125115125;
bin << 12125115125;
bin << 12112511312315125;
I can't post more than this, but that's where the problem is occurring anyway. Number's are just random.
and whats the type of "this"? I guess it's QFile?
What about just something like this?
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";
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
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
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:
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();
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 :)