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 ...
随机推荐
- DS实验题 融合软泥怪-2 Heap实现
题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...
- 总结-mysql
执行sql脚本: source C:/users/liaolongjun/Desktop/miduo_fileinfo.sql; 注意,必须是正斜杆.如果是反斜杆,也会执行,但会报错. 导入数据: L ...
- iOS版本更新的App提交审核发布流程
http://www.2cto.com/kf/201502/378698.html 版本更新的App和新App的发布提交流程略有不同,新的App需要在开发者账号里准备发布证书,添加App的id,关联描 ...
- java 给指定时间加上天数or给当前日期加天数
给指定日期加上天数: /** * 指定日期加上天数后的日期 * @param num 为增加的天数 * @param newDate 创建时间 * @return * @throws ParseExc ...
- c# json TO xml
using System.IO;using System.Text;using System.Xml.Serialization;using System.Xml;using System.Runti ...
- android中如何在低版本(5.0之前)上使用tint(着色)属性
1. 使用app前缀(app:backgroundTint,app:backgroundTintMode),如果使用android前缀,在低版本上是拿不到值的,因为这些属性是5.0以后才加入的. 2. ...
- Hyper-V 2012 R2 故障转移群集之建立域控(AD DS)与加入域
Windows 2012 R2建立域控(AD DS)与加入域 Active Directory概述: 使用 Active Directory(R) 域服务 (AD DS) 服务器角 ...
- 让div支持placeholder属性/模拟输入框的placeholder属性
实现方式:css div:empty:before{ content: attr(placeholder); color:#bbb;}div:focus:before{ content:none; }
- Python之路----------random模块
随机数模块: import random #随机小数 print(random.random()) #随机整数 print(random.randint(1,5))#他会打印5 #随机整数 print ...
- Linux服务器ftp命令找不到
ftp commond not find 先用命令rpm -q vsftpd检查是否安装了ftp服务器 若显示vsftpd-2.2.2-11.el6_4.1.x86_64这样的信息,说明FTP服务器已 ...