qbytearray

QByteArray convert to/from unsigned char *

匿名 (未验证) 提交于 2019-12-03 09:18:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: QByteArray inArray = " ... "; unsigned char *in = convert1(inArray); unsigned char *out; someFunction(in, out); QByteArray outArray = convert2(out); the question is how can I correctly make these conversions (convert1 and convert2). I cannot change someFunction(unsigned char *, unsigned char *), but I have to work with QByteArray here. 回答1: Qt has really great docs , you should use them. If someFunction doesn't modify or store pointer to in data you can use this: QByteArray inArray = " ... "; unsigned char *out; someFunction((unsigned char*)

QByteArray to QString

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 some stuff. else //do other stuff. In the debugger,

c++ passing a const object reference to a function

匿名 (未验证) 提交于 2019-12-03 01:45:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: error: passing 'const QByteArray' as 'this' argument of 'QByteArray& QByteArray::append(const QByteArray&)' discards qualifiers [-fpermissive] since it is a convention to make objects const while passing as function arguments i have done it. but now i am getting an error!!, i dnt want to make the function constant as i have to convert data in qbyte array into short and then append it another array. QByteArray ba((const char*)m_output.data(), sizeof(ushort)); playbackBuffer.append(ba); I really need to pass this array into playbackbuffer ; It

Why doesn't `unique_ptr<QByteArray>` degrade to `QByteArray*`?

夙愿已清 提交于 2019-12-03 01:10:45
问题 I have the following code: msg_buf_ptr = std::make_unique<QByteArray>(); return QDataStream{msg_buf_ptr, QIODevice::WriteOnly}; I am getting the following error: no known conversion for argument 1 from ‘std::unique_ptr<QByteArray>’ to ‘QByteArray*’ But...why? I thought unique_ptr and shared_ptr automatically degrade to raw pointers when passed as arguments to functions taking pointers. If not, why not? If they (usually) do, why does this fail in the case of QByteArray ? I could explicitly

QByteArray类

匿名 (未验证) 提交于 2019-12-03 00:32:02
QByteArray类 提供一个字节数组,QByteArray可用于存储原始字节(包括“\ 0” )和传统的8位 “\ 0” 端接字符串 . 使用QByteArray比使用const char *更方便. QByteArray适合的两个主要情况是当您需要存储原始二进制数据,并且当内存保护至关重要时(例如,使用嵌入式Linux的Qt) 其中包含数据“Hello”: QByteArray ba( "Hello" ) ; 1 以便如果使用一个函数来请求指向底层数据的指针(例如调用data()),那么指出的数据保证被’\ 0’终止。 另一种方法是使用resize()设置数组的大小,并初始化每个字节的数据字节. 返回一个可以在赋值左侧使用的字节的引用。例如: QByteArray ba; ba.resize( 5 ); ba[ 0 ] = 0x3c ; ba[ 1 ] = 0xb8 ; ba[ 2 ] = 0x64 ; ba[ 3 ] = 0x18 ; ba[ 4 ] = 0xca ; //对于只读访问,替代语法是使用at(): for ( int i = 0 ; i < ba. size (); ++i) { if (ba.at(i) >= 'a' && ba.at(i) <= 'f' ) cout << "Found character in range [a-f]" << endl;

QByteArray详解

匿名 (未验证) 提交于 2019-12-03 00:22:01
QByteArray在串口通讯中经常被使用,有一定必要较为全面详细的对QByteArray进行阐述。本文通过以下几个部分加以介绍: 1. 初始化 2. 访问与赋值 3. 添加、删除、插入与替换操作 4. 查找与比较 5. 数据转换与处理 1 初始化 2 访问与赋值 QByteArray ba; ba.resize(6); ba[0] = 0x3c; ba[1] = 0xb8; ba[2] = 0x64; ba[3] = 0x18; ba[4] = 0xca; ba.data()[5] = 0x31; qDebug()<<"[]"<<ba[2]; //[] d qDebug()<<"at()"<<ba.at(2); //at() d qDebug()<<"data()"<<ba.data()[2]; //data() d qDebug()<<"constData()"<<ba.constData()[2]; //constData() d qDebug()<<"constData()"<<ba.constData()[5]; //constData() 1 4 查找与比较 5 数据转换与处理 5.1 Hex转换 QByteArray text = QByteArray::fromHex("517420697320677265617421"); text.data(); //

Why doesn't `unique_ptr<QByteArray>` degrade to `QByteArray*`?

妖精的绣舞 提交于 2019-12-02 13:37:13
I have the following code: msg_buf_ptr = std::make_unique<QByteArray>(); return QDataStream{msg_buf_ptr, QIODevice::WriteOnly}; I am getting the following error: no known conversion for argument 1 from ‘std::unique_ptr<QByteArray>’ to ‘QByteArray*’ But...why? I thought unique_ptr and shared_ptr automatically degrade to raw pointers when passed as arguments to functions taking pointers. If not, why not? If they (usually) do, why does this fail in the case of QByteArray ? I could explicitly call msg_buf_ptr.get() , but that seems like it should be unnecessary. No, this is not a special case; the

can't print readAllStandardOutput correctly decoded

假如想象 提交于 2019-12-02 08:10:08
问题 I have this code to print into a text field all the output from a process: data = self.m_process.readAllStandardOutput() s = str(data) self.m_ui.b_renderOutput.append(s) What I get in the output is this: b'' b'' b'' b'\r\nStarting "C:\\Program Files' b'' b'\\Autodesk\\Maya2018\\bin\\mayabatch.exe"\r\n' b'Initialized VP2.0 renderer {\r\r\n' I'm not able to decode it and print it in the right way. I know that what comes from readAllStandardOutput is a QByteArray 回答1: If you want to convert

QT笔记:QByteArray的使用

十年热恋 提交于 2019-11-30 12:40:18
** QT笔记:QByteArray的使用 ** 1 .QByteArray可以用来存储原始二进制字节和8-bits字符,一般在需要传输原始数据和内存资源短缺时使用(嵌入式linux Qt) 2 .QByteArray存储的是char型字符,继承自QMemArray< char >,但QByteArray提供的数组操作,比char更方便 3 .QString转Qchar时,需要经过 QByteArray,不能用下面的转换形式char *data = str.toLatin1().data();因为这样的话,str.toLatin1()得到的QByteArray类型结果就不能保存(切记!!!),正确用法如下: hex= str.toLatin1();//把string字符串转换为字节流 char= QByteArray::fromHex(hex);//把字节流转换成char 4 .QByteArray数组 /*****************定义赋值数组*****************/ QByteArray ba; ba.size(5); //size()大小,不包含'\0' ,qstrlen()包含'\0' ba[0]=0x10; ba[1]=0xc1; …… ba[4]=0xbf; /******************访问数组*******************/ for

How to convert QByteArray to std::istream or std::ifstream?

橙三吉。 提交于 2019-11-29 15:28:11
I want to create istream from QByteArray at runtime, without saving a physical file in memory of QByteArray . I found that there are many ways to do the opposite conversion, i.e. istream to QByteArray , but not this one. How to accomplish that? To read via std::istringstream from QByteArray seems quite easy: testQByteArray-istream.cc : #include <iostream> #include <sstream> #include <QtCore> int main() { qDebug() << "Qt Version:" << QT_VERSION_STR; // make a QByteArray QByteArray data("Hello Qt World."); // convert to std::string std::istringstream in(data.toStdString()); // read from