conversion of hex data in unsigned char array to QString.
-
Hi, Need to convert unsigned char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6}; to QString and wanted to store in QString str.
And it should be able to print like "13C5380380E6DC34" with qDebug()<<str;
Please suggest. -
You do you want to print like this ? Do you want the data like this in QString ? Since these are special characters, even if you print you will see some funny characters. After conversion just ensure that your data is fine. Tried something like this. Google hit may give many answers.
-
Does this work?
const QByteArray array(QByteArray::fromHex(buff)); const QString string(array); qDebug() << string << array;
Yea, like @dheerendra says, if the data is not really hex, the output will be weird characters anyway. I suggest you keep the data in QByteArray, usign QString for this is an overkill.
-
@Tharapathi
jep the others are correct, QByteArray is the way to go.it has a constructor that accepts a const char array,
https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1
and has a convenient toHex() function that prints (with QDebug) the values like you want it to
https://doc.qt.io/qt-5/qbytearray.html#toHexand QString accepts a QByteArray in its constructor
https://doc.qt.io/qt-5/qstring.html#QString-8 -
@Tharapathi Try this
char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6}; qDebug() << QByteArray(buff, sizeof(buff)).toHex().constData();
-
@KroMignon said in conversion of hex data in unsigned char array to QString.:
char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6};
qDebug() << QByteArray(buff, sizeof(buff)).toHex().constData();Nearly perfect. You can omit the
constData()
for qDebug, though.