简体   繁体   English

如何在Qt 5.3中将QByteArray转换为字符串?

[英]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: 我正在使用一些函数将QVector's转换为QByteArray's ,例如:

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 ? 现在,我有了QByteArray ,需要将其放入文本文件中,如何将其转换为QString

I already tried the simplest way: 我已经尝试了最简单的方法:

QString myString(data); // data - QByteArray

But myString is always empty. 但是myString始终为空。

I also found the toStdString() function in the documentation, but it was introduced only in Qt 5.4 . 我还在文档中找到了toStdString()函数,但是它仅在Qt 5.4中引入。

I'm using Qt 5.3 . 我正在使用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. 注意 :这样做的总体目的是从SQLite数据库生成包含所有内容的SQL文件。 My double vector is converted to QByteArray and stored as BLOB into the database (using the serialize function). 我的双重向量被转换为QByteArray并作为BLOB存储到数据库中(使用序列化功能)。 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. 现在,我需要使用BLOB格式的数据生成SQL文件,然后可以将其直接导入到另一个数据库中。

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. 但是,当您将字节数组传递给QString构造函数时,QString试图将字节数组解释为表示字符串的数据,例如ASCII字符代码的数组。

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. 某些字符串类可能会允许您执行此操作,并创建一个充满垃圾的实例,但是QString似乎正在执行一些基本的错误检查,并通过给您一个空字符串来帮助您。

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 : 使用QTextCodecQByteArray转换为QString ,这是来自官方文档的示例:

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: 为了使示例工作,您需要在序列化期间将double转换为QString

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() : 如果您不想将单个数字转换为字符串,则还可以使用byteArray.toHex()QByteArray “字符串化”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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