QByteArray of char* to QString
-
Here is my code:
//snip if(busOn){ long id; QString l; QString canMessage; unsigned char data[8]; unsigned int dlc, flags; unsigned long timestamp; canStatus status = canReadWait(canHandle, &id, data, &dlc, &flags, ×tamp, 100); QByteArray msgData = QByteArray((char*)data, 8); if (status == canOK){ QTextStream(&l) << QString("%1").arg(id, 8, 16, QLatin1Char( '0' )) << msgData; } m_ui->textEditCanIn->append(l); if (canMessage != "") { qDebug() << canMessage; //emit canMessageOut(canMessage); }
The debug statement at the end prints out this:
"18ff0301\u0004\u0000\u0000\u0000\u0000\u0000\u0000\u0000"The first part of the message is fine (the message ID), and the second part contains the data I expect to see (struggled for a bit to get there), but I need to put it into a QString format like this:
"18ff0301[0x]0400000000000000"so I can send it to another function for processing (the emit canMessageOut). I've been struggling with how to format the message and having no luck converting a QByteArray of char* to a QString.
-
If you want to show the content of the byte array as hex values so the user can read it, then you should use toHex()
-
Hi @Christian-Ehrlicher ,
I tried this:
QByteArray msgData = QByteArray(data).toHex();
(had to change from unsiged char to char)
but then the debug statement prints nothing for the data (only the ID):
"18ff0301"
-
Hi @Christian-Ehrlicher ,
I tried this:
QByteArray msgData = QByteArray(data).toHex();
(had to change from unsiged char to char)
but then the debug statement prints nothing for the data (only the ID):
"18ff0301"
@MScottM said in QByteArray of char* to QString:
but then the debug statement prints nothing for the data
Because now you don't pass the size of your char* array to the QByteArray ctor.
-
Dang, that was it. I swear I tried that yesterday, but I probably had it wrong as I was tired and throwing code around trying to make it work.
Thanks once again for the help!
This post is deleted!