简体   繁体   中英

How can I convert QByteArray to string in Qt 5.3?

I am using some functions to convert QVector's to QByteArray's , for example:

QByteArray Serialize::serialize(QVector<double> data)
{
    QByteArray byteArray;
    QDataStream out(&byteArray, QIODevice::WriteOnly);
    out << data;
    return byteArray;
}

void Serialize::deserialize(QByteArray byteArray, QVector<double> *data)
{
    QDataStream in(&byteArray, QIODevice::ReadOnly);
    in >> *data;
}

Now, that I have the QByteArray I need to put it in a text file, how can I convert it to QString ?

I already tried the simplest way:

QString myString(data); // data - QByteArray

But myString is always empty.

I also found the toStdString() function in the documentation, but it was introduced only in Qt 5.4 .

I'm using Qt 5.3 .

Follows a complete example:

#include <QCoreApplication>

#include <QDebug>
#include <QVector>
#include <QByteArray>
#include <QDataStream>

QByteArray serialize(QVector<double> data)
{
    QByteArray byteArray;
    QDataStream out(&byteArray, QIODevice::WriteOnly);
    out << data;
    return byteArray;
}

void deserialize(QByteArray byteArray, QVector<double> *data)
{
    QDataStream in(&byteArray, QIODevice::ReadOnly);
    in >> *data;
}

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

    QVector<double> data;
    data << 1.1 << 2.2 << 3.3 << 4.4 << 5.5 << 6.6 << 7.7 << 8.8 << 9.9;

    QByteArray byteArray = serialize(data);
    QVector<double> dataConverted;
    deserialize(byteArray, &dataConverted);

    qDebug() << "Data:";
    qDebug() << data;
    qDebug() << "ByteArray:";
    QString test(byteArray);
    qDebug() << test;
    qDebug() << "Data Converted:";
    qDebug() << dataConverted;

    return a.exec();
}

Note : The general objective of this is to generate a SQL file with all content from the SQLite database. My double vector is converted to QByteArray and stored as BLOB into the database (using the serialize function). When I need to load it from the database I use the deserialize function to convert to a double vector again. Now I need to generate the SQL file with the data in the BLOB format, then I can directly import it into another database.

The problem is that a byte array is type-agnostic data type, it simply represents a collection of individual bytes in memory. In your example code you are creating the byte array from a vector of doubles, and then converting back to another vector of doubles. No problem.

However when you pass the byte array into the QString constructor, the QString is trying to interpret the byte array as data that represents a string, for example an array of ASCII character codes.

Some string classes might let you do this, and create an instance filled with garbage, however QString appears to be doing some basic error checking and helping you out by giving you an empty string.

As for some code to print out the contents of a byte array of doubles, the deserialize method you've provided is not too bad an example.

Use QTextCodec to convert from QByteArray to QString , here's an example from official docs :

QByteArray encodedString = "...";
QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
QString string = codec->toUnicode(encodedString);

And to make the example work, you need to convert the doubles to QString s during serialization:

QByteArray serialize(QVector<double> data)
{
    QByteArray byteArray;
    QDataStream out(&byteArray, QIODevice::WriteOnly);
    for (double d : data) {
        out << QString::number(d);
    }
    return byteArray;
}

If you don't want to convert individual numbers to string, you can also "stringify" the QByteArray with byteArray.toHex() :

qDebug() << "ByteArray:";
QString test(byteArray.toHex());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM