How can I convert QByteArray to string in Qt 5.3?

自作多情 提交于 2019-12-08 01:36:15

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/41618222/how-can-i-convert-qbytearray-to-string-in-qt-5-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!