0

I want to display the value of QbyteArray like how qDebug() displays it.

qDebug()<<byteArray    ===  Displays -> "\x8E\xA9\xF3\xA5"

how do you grab this QbyteArray into a QString, when i do the convertion found online it gives me "????" as an output .

I would like the content of the QString is the same as the output of the QDebug();

"\x8E\xA9\xF3\xA5"

so that QString string would contain "\x8E\xA9\xF3\xA5"

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jake quin
  • 738
  • 1
  • 9
  • 25

1 Answers1

1

Build a QDebug object using the constructor:

QDebug::QDebug(QString *string)

Constructs a debug stream that writes to the given string.

Example:

#include <QApplication>
#include <QDebug>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QLabel label;
    QByteArray ba("\x8E\xA9\xF3\xA5");
    QString res;
    QDebug db(&res);
    db << ba;
    label.setText(res);
    label.show();

    return a.exec();
}

enter image description here


Update:

without "\x", use toHex():

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QLabel label;
    QByteArray ba("\x8E\xA9\xF3\xA5");
    label.setText(ba.toHex());
    label.show();

    return a.exec();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you very much that helped a Lot do you mind a follow up question?? is there an east way of removing the "\x" so it would leave "8E A9 F3 A5" without having to do it manually? Again thank you very much. – Jake quin Jul 18 '18 at 18:09
  • @Jack mmm, that's easier, why did not you point that out from the beginning? – eyllanesc Jul 18 '18 at 18:11
  • I know this might be a late reply but thank you very much, this what i actually did :) – Jake quin Aug 03 '18 at 14:04
  • @Jack I recommend you consider taking the time to respond as we take the time to help you. :) – eyllanesc Aug 03 '18 at 14:07