Solved QByteArray Initialization
-
@vivekyuvan said in QByteArray Initialization:
Hi@mostefa The Error message gone and I got the output thanks. But I want to send the array to emit signal? is that possible ? I am newbie to QT soft/Development guide me to resolve this problem
You can actually send array in signal,
let's say you have the following signal :
void sendByteArray(QByteArray);// on your .h file
inside your testArrayInitialization you can do something like this :
QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6); emit sendByteArray(m_data);
If this is not what you want , can you explain what you want to do?
-
@mostefa thanks The problem is solved
-
You are welcome =) , you can change the state of your post to solved
-
What about:
QByteArray text = QByteArray::fromHex("517420697320677265617421");
-
-
Hi friend, use this:
#include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QByteArray data; data.append((char)0x0c); data.append((char)0x06); data.append((char)0x04); data.append((char)0x04); data.append((char)0x02); data.append((char)0x00); data.append((char)0xFF); printf("%d\n", data.size()); printf("%s\n", data.toHex().toStdString().c_str()); return a.exec(); }
-
@joaopagotto This is actually the same he already did. He asked for something like this or similar:
QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
Also there is no need to use printf in C++. Instead of
printf("%d\n", data.size());
you can just write
qDebug() << data.size();
or C++ without Qt
std::cout << data.size();
-
This post is deleted! -
@jsulm said in QByteArray Initialization:
@titan83 This is just a string containing ASCII characters.
Of course @titan83 has a point here. His string is made of ASCII characters, but he uses QByteArray::fromHex(). This may not be the fastest operation, but allows to read the data from UI or an ASCII file, for example.
-
You can basically use a C++11 initializer list the following way (I've added a char higher than 0x7f and initializtion by variables):
char v1 = 0x06, v2 = 0x04; QByteArray ba(std::begin<char>({0x0c, v1, v2, v2, 0x02, 0x00, '\xf3'}), 7);