如何在Qt 5.3中将QByteArray转换为字符串? [英] How can I convert QByteArray to string in Qt 5.3?

查看:1653
本文介绍了如何在Qt 5.3中将QByteArray转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些函数将QVector's转换为QByteArray's,例如:

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

现在,我有了QByteArray,需要将其放入文本文件中,如何将其转换为QString?

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

但是myString始终为空.

我还在文档中找到了toStdString()函数,但仅在 Qt 5.4 中引入了该功能.

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

我正在使用 Qt 5.3 .

遵循一个完整的示例:

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

注意:此操作的一般目的是生成一个 SQL 文件,其中包含来自 SQLite 数据库的所有内容.我的双重向量被转换为QByteArray并以 BLOB 的形式存储到数据库中(使用序列化功能).当我需要从数据库中加载它时,我可以使用反序列化功能再次将其转换为双精度向量.现在,我需要使用 BLOB 格式的数据生成 SQL 文件,然后可以将其直接导入到另一个数据库中.

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.

但是,当您将字节数组传递给QString构造函数时,QString试图将字节数组解释为表示字符串的数据,例如ASCII字符代码的数组.

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似乎正在执行一些基本的错误检查,并通过给您一个空字符串来帮助您.

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.

这篇关于如何在Qt 5.3中将QByteArray转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆