I work with QT Creator (MinGW 5.3.0 C++, Windows) and want to write some serial-connection hex-data. I found a way but there is something wrong. Only one hex-data he convert wrong.
I hope my code will help to understand.
static const unsigned char mytestUC[] = {0x04,0x31,0xD2,0x01, 0xA1};
enum { NBYTES = sizeof(mytestUC) };
const char *mytest = reinterpret_cast<const char*>(mytestUC);
QByteArray testdata = QByteArray::fromRawData(mytest, sizeof(mytest)+1);
qDebug()<<"testdata "<<testdata;
I would think the output should be: testdata "\x04\31\xD2\x01\xA1"
But in real it looks like this: testdata "\x04""1\xD2\x01\xA1"
I tried some other way to write my hex-data directly in the QBytearray with append.
testdata .append((char) 0x04);
testdata .append((char) 0x31);
testdata .append((char) 0xD2);
testdata .append((char) 0x01);
testdata .append((char) 0xA1);
Why does the Programm convert only the the 0x31 in the wrong way and how can I make it better? Is there some easier way to write hex-data in QBytearray?