Jake quin
Jake quin

Reputation: 742

Preserve QbyteArray to Qstring

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"

Upvotes: 0

Views: 404

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

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();
}

Upvotes: 1

Related Questions