如何将QVector转换为QBytearray ?

时间:2021-02-28 04:18:24

I would like to convert QVector<double> to QBytearray, and I have no idea on how to do this. I tried this but the program crashes:

我想把QVector< >转换为QBytearray,我不知道怎么做。我试过了,但是程序崩溃了:

QVector<double> vec;
QByteArray arr = QByteArray::fromRawData(reinterpret_cast<const char*>(vec),vec.size());
for(int i = 0; i< vec.size(); i++)
    arr.append(reinterpret_cast<const char*>(vec.data(&numberOfData),sizeof(double));

Can someone tell me how to do it properly?

有人能告诉我怎么做吗?

3 个解决方案

#1


1  

You must pass through a native array, so your QByteArray will receive a sequence of contiguous bytes.

您必须通过一个本机数组,因此您的QByteArray将接收一个连续字节序列。

double *bytes = new double[vec.size()];
for (int i = 0; i < vec.size(); ++i) {
   bytes[i] = vec[i];
}
QByteArray array = QByteArray::fromRawData(reinterpret_cast<void*>(bytes));
delete []bytes;

Disclaimer: untested code.

免责声明:未测试的代码。

--- Update ---

推荐- - - - - - - - - - - -更新

As leemes correctly pointed out, you DON'T need to allocate and copy a byte array, QVector already provides two access functions to raw data. So you can simply use data() and constData(). Please see his response.

正如leemes正确指出的那样,您不需要分配和复制一个字节数组,QVector已经为原始数据提供了两个访问函数。因此,您可以简单地使用data()和constData()。请参阅他的反应。

#2


8  

You can use QVector::constData to get a pointer to the (const) raw data contained in the vector. However, you also need to multiply the size by the size of a single entry, i.e. sizeof(double). You don't need a loop afterwards like in your code.

您可以使用QVector::constData来获得指向向量中包含的(const)原始数据的指针。但是,您还需要将大小乘以单个条目的大小,即sizeof(double)。在以后的代码中不需要循环。

QByteArray data = QByteArray::fromRawData(
        reinterpret_cast<const char*>(vec.constData()),
        sizeof(double) * vec.size()
    );

You could also use QDataStream to do the conversion, resulting in a much cleaner code, which also takes care of potential byte ordering issues.

您还可以使用QDataStream来进行转换,从而产生一个更简洁的代码,这也会处理潜在的字节排序问题。

QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
for (auto x : vec)
    stream << x;

#3


0  

Today this conversion is even simpler:

今天这种转换更加简单:

QVector<double> vec;
// populate vector
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << vec;

#1


1  

You must pass through a native array, so your QByteArray will receive a sequence of contiguous bytes.

您必须通过一个本机数组,因此您的QByteArray将接收一个连续字节序列。

double *bytes = new double[vec.size()];
for (int i = 0; i < vec.size(); ++i) {
   bytes[i] = vec[i];
}
QByteArray array = QByteArray::fromRawData(reinterpret_cast<void*>(bytes));
delete []bytes;

Disclaimer: untested code.

免责声明:未测试的代码。

--- Update ---

推荐- - - - - - - - - - - -更新

As leemes correctly pointed out, you DON'T need to allocate and copy a byte array, QVector already provides two access functions to raw data. So you can simply use data() and constData(). Please see his response.

正如leemes正确指出的那样,您不需要分配和复制一个字节数组,QVector已经为原始数据提供了两个访问函数。因此,您可以简单地使用data()和constData()。请参阅他的反应。

#2


8  

You can use QVector::constData to get a pointer to the (const) raw data contained in the vector. However, you also need to multiply the size by the size of a single entry, i.e. sizeof(double). You don't need a loop afterwards like in your code.

您可以使用QVector::constData来获得指向向量中包含的(const)原始数据的指针。但是,您还需要将大小乘以单个条目的大小,即sizeof(double)。在以后的代码中不需要循环。

QByteArray data = QByteArray::fromRawData(
        reinterpret_cast<const char*>(vec.constData()),
        sizeof(double) * vec.size()
    );

You could also use QDataStream to do the conversion, resulting in a much cleaner code, which also takes care of potential byte ordering issues.

您还可以使用QDataStream来进行转换,从而产生一个更简洁的代码,这也会处理潜在的字节排序问题。

QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
for (auto x : vec)
    stream << x;

#3


0  

Today this conversion is even simpler:

今天这种转换更加简单:

QVector<double> vec;
// populate vector
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << vec;