qbytearray


Convert QString into QByteArray with either UTF-8 or Latin1 encoding

只谈情不闲聊 提交于 2020-01-01 05:18:25
问题 I would like to covert a QString into either a utf8 or a latin1 QByteArray, but today I get everything as utf8. And I am testing this with some char in the higher segment of latin1 higher than 0x7f, where the german ü is a good example. If I do like this: QString name("\u00fc"); // U+00FC = ü QByteArray utf8; utf8.append(name); qDebug() << "utf8" << name << utf8.toHex(); QByteArray latin1; latin1.append(name.toLatin1()); qDebug() << "Latin1" << name << latin1.toHex(); QTextCodec *codec =

QByteArray to QString

大兔子大兔子 提交于 2019-12-30 00:15:24
问题 I'm having issues with QByteArray and QString . I'm reading a file and stores its information in a QByteArray . The file is in unicode, so it contains something like: t\0 e\0 s\0 t\0 \0 \0 I'm trying to compare this value to my specified value, but it fails, because in the debugger I see it's not an unicode string. The code will explain everything: QByteArray Data; //contains unicode string "t\0 e\0 s\0 t\0 \0 \0" QString myValue = "test"; //value to compare. if(Data.contains(myValue)) //do

Convert QByteArray from big endian to little endian

孤街醉人 提交于 2019-12-24 23:03:52
问题 I think I’m a kind of at a loss here. I trying such a simple thing, I can’t believe that there is nothing build-in Qt (using Qt 5.6.2). I try to convert the data inside a QByteArray from big endian to little endian. Always starts with the same test QByteArray like this. QByteArray value; value.append(0x01); value.append(0x02); value.append(0x03); qDebug() << "Original value is: " << value.toHex(); // “010203” like expected What I need is the little endian, which means the output should be

Qt编写图片及视频TCP/UDP网络传输

无人久伴 提交于 2019-12-16 10:50:31
一、前言 很多年前就做过类似的项目,无非就是将本地的图片上传到服务器,就这么简单,其实用http的post上传比较简单容易,无需自定义协议,直接设置好二进制数据即可,而采用TCP或者UDP通信的话,必须自定义协议,因为不知道什么时候数据接收完了是完整的图片数据,可能同时在发送很多图片数据,而且还不能区分收到的图片是哪个客户端发来的,TCP长连接的话,还需要有心跳来检测连接,所以必须自定义一套协议来支撑通信,这套协议采用的是上海监管平台的通信协议格式,拓展性比较强,其中头部信息包括了类型+当前完整包的数据长度,这个类型就是通信协议的标识,这样下次来一个其他类型的比如楼宇对讲可以叫IDOOR,服务端根据这个标识就能知道采用何种解析算法来处理后面的数据,而当前完整包的数据长度可以用来处理收到的数据,只有该长度的数据才表示接收完成一个完整的图片数据,再去解码处理。当传输的图片到了一定速度的时候比如一秒钟传输20张图片,其实就相当于传输视频了,一般人的肉眼看到一秒钟20张图片基本上认识就是视频了。 TCP理论上是稳定的连接,不会丢包,也不会随便一个包插入到一个包的中间,肯定能保证一个数据包的完整性,TCP连接也分两种,一种是长连接,一旦连接了就一直通信,主要用在频繁通信的场景中比如实时上传,还有一种叫短连接,客户端发完数据或者服务端接收完数据就立即断开连接

Best practice: how to interpret/process QDataStream?

前提是你 提交于 2019-12-14 02:33:05
问题 I need to process streamed binary data (QDataStream) of defined structure created by another non-Qt program. I wonder what is the best practice to interpret that data. Let's say the data is structured (struct definition provided) in telegrams as follows and cannot be changed by myself: 4 bytes header | 2 bytes sequence number | 1 byte checksum | 10 bytes data I see the following possibilities to handle the data in a "Telegram" class: The Telegram class has a private QByteArray member variable

Append quint16/unsigned short to QByteArray quickly

廉价感情. 提交于 2019-12-12 16:47:37
问题 In my project I'm working with QByteArrays appending data to them as the program goes. Most of the time, a simple quint8 gets appended just fine using QByteArray::append() . But when a quint16 gets appended, only 1 byte gets appended instead of 2. QByteArray ba = QByteArray::fromHex("010203"); quint number(300);//300 in hex is 012c ba.append(number);//What should be appended instead of just number? //the current incorrect result is ba.toHex() == "0102032c" //the desired result is ba.toHex() =

QT中的char、float、QbyteArray数据间的转换

前提是你 提交于 2019-12-11 10:45:04
在QT开发上位机与单片机通信时,因通信协议包的数据类型有“char”和“float”类型,特此记录。协议包类型如下: typedef struct{ char data1; char data2; char data3; char data4; char dataGroup[5]; float value; char data7; }DataInit; 因为代码中需要计算每个字节的内存地址的数据的累加和,char类型的可以直接进行累加,而float类型的数据值无法直接进行累加,如果进行累加数据将会出错,这时就需要将float类型转化为char型。有以下两种方式: 1、可以重新定义协议包将float类型的数据更改为char 类型数组,例如: typedef struct{ char data1; char data2; char data3; char data4; char dataGroup[5]; char floatData[4]; char data7; }DataInit; 因为float本身在内存中分配就为4个字节,所以可在定义时直接将其定义为内存大小是4个字节的char类型的数组,在应用时可再将float数据转换为char类型并存入char类型数组中,具体操作方式参考第二种方式。 2、原本的数据包类型不变,在应用时定义一个char类型的指针

How to unpack 32bit integer packed in a QByteArray?

梦想的初衷 提交于 2019-12-10 22:14:28
问题 I'm working with serial communication, and I receive 32bit integers in a QByteArray , packed in 4 separate bytes (little-endian). I attempt to unpack the value from the 4 bytes using QByteArray::toLong() but it fails the conversion and returns the wrong number: quint8 packed_bytes[] { 0x12, 0x34, 0x56, 0x78 }; QByteArray packed_array { QByteArray(reinterpret_cast<char*>(packed_bytes), sizeof(packed_bytes)) }; bool isConversionOK; qint64 unpacked_value { packed_array.toLong(&isConversionOK) };

Load QPixmap from QByteArray in Qt?

风格不统一 提交于 2019-12-10 12:59:05
问题 I have a byte array with the contents of an image (in png/bmp or some other format). How can I load it into a QPixmap? 回答1: bool QPixmap::loadFromData ( const QByteArray & data, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor ) Format here is string literal like "PNG" or something similar QPixmap p; QByteArray pData; // fill array with image if(p.loadFromData(pData,"PNG")) { // do something with pixmap } 回答2: You should use the folowing, where your bytes are in the

Convert from QByteArray to array of double

半腔热情 提交于 2019-12-10 09:19:25
问题 I have an array of double : QVector<double> Y(count); I need to pack it to QByteArray to send via Ethernet. So I did it. It was not too hard: QByteArray line; line.clear(); line.append(QByteArray::fromRawData(reinterpret_cast<const char*>(Y.data()), count*sizeof(double))); I try use this code to unpack the data from QByteArray recv : QVector<double> data((line.size())/sizeof(double)); QByteArray dou(sizeof(double),0x0); for(int i = 0; i<data.count(); i++){ dou = recv.mid(i*sizeof(double)

工具导航Map