QDataStream和QByteArray
一个写操作可以参考:
QDataStream &operator >>(QDataStream &in, SerializedMessage &message)
{
qint32 type;
qint32 dataLength;
QByteArray dataArray;
in >> type >> dataLength;
dataArray.resize(dataLength); // <-- You need to add this line.
int bytesRead = in.readRawData(dataArray.data(), dataLength);
// Rest of function goes here.
}
void SomeClass::slotReadClient() { // slot connected to readyRead signal of QTcpSocket
QTcpSocket *tcpSocket = (QTcpSocket*)sender();
while(true) {
if (tcpSocket->bytesAvailable() < ) {
break;
}
char buffer[]
quint32 peekedSize;
tcpSocket->peek(buffer, );
peekedSize = qFromBigEndian<quint32>(buffer); // default endian in QDataStream
if (peekedSize==0xffffffffu) // null string
peekedSize = ;
peekedSize += ;
if (tcpSocket->bytesAvailable() < peekedSize) {
break;
}
// here all required for QString data are available
QString str;
QDataStream(tcpSocket) >> str;
emit stringHasBeenRead(str);
}
}
QString占两字节,转成一个字节可以用toUtf8()。
Per the Qt serialization documentation page, a QString is serialized as: - If the string is null: 0xFFFFFFFF (quint32)
- Otherwise: The string length in bytes (quint32) followed by the data in UTF-.
If you don't like that format, instead of serializing the QString directly, you could do something like stream << str.toUtf8();
How I can remove it, including last null byte?
You could add the string in your preferred format (no NUL terminator but with a single length header-byte) like this: const char * hello = "hello";
char slen = strlen(hello);
stream.writeRawData(&slen, );
stream.writeRawData(hello, slen);
QVariantMap myMap, inMap;
QByteArray mapData; myMap.insert("Hello", );
myMap.insert("World", ); QDataStream outStream(&mapData, QIODevice::WriteOnly);
outStream << myMap;
qDebug() << myMap;
QDataStream inStream(&mapData, QIODevice::ReadOnly);
inStream >> inMap;
qDebug() << inMap;
QDataStream和QByteArray的更多相关文章
- 【转】从QDataStream向QByteArray中写入数据时的注意点(QT)
最近发现从QDataStream向QByteArray中写入数据常常是写不进去的,通过查看QT的源码: QDataStream &operator>>(QDataStream &a ...
- Qt串行化的输入和输出(使用QDataStream读写QByteArray,对QIODevice直接起作用)
参考https://lug.ustc.edu.cn/sites/qtguide/ 今天看了一个介绍Qt串行化的介绍,感觉很受益,就记录了下来. 串行化(Serialization)是计算机科学中的一个 ...
- QFile QDataStream QTextStream
#include <QCoreApplication> #include <QMap> #include <QFile> #include <QDir> ...
- QDataStream对QVector的序列化
最近发现QDataStream这个好东东,序列化发送数据很方便,与大家分享一下. 客户端: line.h #ifndef LINE_H #define LINE_H #include <QStr ...
- QTcpSocket 及 TCP粘包分析
----我的生活,我的点点滴滴!! 这两天用Qt简单的实现一个tcp多线程client,在此记录下知识. 一.长连接与短连接 1.长连接 Client方与Server方先建立通讯连接,连接建立后不断开 ...
- Qt5_TCP_Client01
ZC: 代码来自<<Qt及Qt Quick开发实战精解>>“代码\src\5\5-3”(“代码\src\5\5-4”里面的代码差不多,不知有何差别...貌似应该是更为完善) Z ...
- 4.关于QT中的QFile文件操作,QBuffer,Label上添加QPixmap,QByteArray和QString之间的区别,QTextStream和QDataStream的区别,QT内存映射(
新建项目13IO 13IO.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets network CON ...
- (十四)QFile操作,QByteArray,文件流操作,QTextStream,QDataStream,QFileInfo, QIODevice
QFile f 1.readall #include "widget.h" #include "ui_widget.h" #include <QFileD ...
- 4.关于QT中的QFile文件操作,QBuffer,Label上加入QPixmap,QByteArray和QString之间的差别,QTextStream和QDataStream的差别,QT内存映射(
新建项目13IO 13IO.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets network CON ...
随机推荐
- python 2.43 升级到2.7
[root@GW1 bin]# lsb_release -aLSB Version: :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3. ...
- 10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...
- HTML中head里的内容经浏览器解析后全到body里了
HTML中head里的内容经浏览器解析后全到body里了 修改完代码后,用chrome审查元素,head里的内容都到body中去了 http://bbs.csdn.net/topics/3802586 ...
- android中使用startactivityforresult跳转Activity后需要重写onBackPressed()方法
在android项目中经常会用startactivityforresult从一个Activity跳转到另一个Activity(这里指Activity_A和Activity_B),你可以从Activit ...
- 恢复 混淆后的 stacktrace 文件
./tools/proguard/bin/retrace.sh /Users/admin/Downloads/ProguardSample/app/build/outputs/mapping/rele ...
- Python--循环语句
Python 循环语句 循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式: Python提供了for循环和while循环(在Python中没有do..while循 ...
- 【转】Apache Digest验证
Apache默认使用basic模块验证,都是明文传输,不太安全,所以本文使用Digest来验证,以提高安全性. 1.Apache配置: A.目录权限配置 Alias /nagios "/us ...
- Contributing to the C++ Core Guidelines
Contributing to the C++ Core Guidelines "Within C++ is a smaller, simpler, safer language strug ...
- 设置更新源和下载ferret
kali无法定位软件包 解决: deb http://http.kali.org/kali kali-rolling main non-free contrib kali可用的官方更新源(cd /et ...
- SpringMVC整合Hibernate实现增删改查之按条件查询
首先我贴出我项目的结构,只完成了条件查询的相关代码,增删改没有写. 1.新建一个动态Web工程,导入相应jar包,编写web.xml配置文件 <context-param> <par ...