Skip to content

Instantly share code, notes, and snippets.

@xavery
Created May 8, 2017 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavery/4a59ac57822f9eea98bc53f49c280758 to your computer and use it in GitHub Desktop.
Save xavery/4a59ac57822f9eea98bc53f49c280758 to your computer and use it in GitHub Desktop.
Serializing and deserializing a QByteArrayList to/from a QDataStream
#include <QByteArray>
#include <QByteArrayList>
#include <QDataStream>
#include <QDebug>
#include <QFile>
#include <cstring>
int main(int argc, char *argv[]) {
if (argc != 2) {
return 1;
}
QFile f("foo.bin");
f.open(QIODevice::ReadWrite);
QDataStream s(&f);
if (::strcmp(argv[1], "read") == 0) {
QByteArrayList y;
s >> y;
qDebug() << y;
} else if (::strcmp(argv[1], "write") == 0) {
auto x = QByteArrayList{{"\xaa\x55", 2}, {"\xcf", 1}, {"\x00\xff\x80", 3}};
s << x;
} else {
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment