将char与数据流一起复制到QbyteArray包含一些额外的字节
copying char to QbyteArray with datastream contains some extra bytes
问题概述:
操作系统:Ubuntu
我使用qt实用程序从远程机器接收视频数据(远程机器使用gstreamer发送实时数据),并将数据写入端口,例如5000。
端口5000已绑定到另一个gstreamer实用程序。该实用程序监听端口5000并将数据转换为视频流。很明显,事情不太正常,我无法观看视频。所以我有两个问题:
1) 使用Qt实用程序,尽管端口绑定到gstreamer实用程序,但写入端口5000是否合法。
2) 我正在使用第三方库及其api从外部源接收数据。数据存储在字符数组中。若我将其转换为qbyterarray,那个么qbyterArray的大小和char缓冲区的大小相同。示例
rc = zco_receive_source(sdr, &reader, bytesToRead, buffer); // this is 3rd part function
qDebug() << " copy buffer size =" << rc; // genrally this size is 1412
QByteArray msgRecvd;
msgRecvd = QByteArray(reinterpret_cast<char*>(buffer), rc);
if(! msgRecvd.isEmpty() )
{
qDebug() << " size of msgRecv =" << msgRecvd.size();// again 1412
udpSock->writeDatagram( msgRecvd, QHostAddress::LocalHost, 5000 );
}
但是,如果我使用QDataStream,那么QbyteArray会获得4个额外的字节。下方显示的代码
rc = zco_receive_source(sdr, &reader, bytesToRead, buffer); // this is 3rd part function
qDebug() << " copy buffer size =" << rc; // genrally this size is 1412
QByteArray msgRecvd;
QDataStream dataStream( &msgRecvd, QIODevice::WriteOnly );
dataStream.writeBytes( ( const char* )buffer, rc );
if(! msgRecvd.isEmpty() )
{
qDebug() << " size of msgRecv =" << msgRecvd.size();// got 4 extra bytes .. size is 1415. why ???
udpSock->writeDatagram( msgRecvd, QHostAddress::LocalHost, 5000 );
}
我想知道为什么QbyteArray有额外的字符,我需要串行化数据才能将其转发到5000端口吗?
回答第二个问题:尝试QDataStream::writeRawData()。
表单Qt文档:
QDataStream & QDataStream::writeBytes(const char * s, uint len)
Writes the length specifier len and the buffer s to the stream and returns a reference to the stream.
The len is serialized as a quint32, followed by len bytes from s. Note that the data is not encoded.
相关文章:
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 一种从内存中删除 UTF 字节的方法?
- 一种将 Dart 中的字节数据转换为 C++ 中的无符号字符*的有效方法?
- 使用字节向量作为其他类型的原始存储是一种好的做法吗
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 当我在结构中包含多个数组时,我的程序会跳过一堆代码
- 搜索字符串是否至少包含一次从 0 到 9 的所有数字的最有效方法
- 至少包含一次字符 X 的 S 的不同子字符串的数量
- C++包含一组对象的类
- 我想在C++项目中包含一种脚本语言.Lua vs Bison/Yacc
- 为什么我可以在多个包含const int的cpp文件中包含一个头文件,而不会出现编译器错误
- 用C++读取包含空字节的二进制文件
- 有没有办法将源代码格式化为每行只包含一条语句
- 双链接列表总是只包含一条记录
- 页眉应包含一次
- 头文件在整个程序中仅包含一次