Qt - QStringList 到 QByteArray

Qt - QStringList to QByteArray

QStringList 转换为 QByteArray 的最有效方法是什么?

我找到了许多类似转换的例子,但从来没有我需要的例子,或者只是不必要地太复杂了。


代码清理字符串输入,然后将其转换为 QByteArray。我正在为后半部分苦苦挣扎。

void MainWindow::on_suicideButton_clicked()
{
    QString input = ui->lineEdit->text();
    try{
        if(ui->lineEdit->text().contains("0x")||ui->lineEdit->text().contains(",")||ui->lineEdit->text().contains("-"))
        {
            input = input.replace("0x","");
            if (input.contains(',')) input = input.replace(",", "");
            // QString trim = input.trimmed();
            // input = trim;//.split(',', ' ', '-');
            QStringList inputArray = input.split('-');
            QByteArray output = inputArray;
        }
        //printf("%s\n", input);

        ui->lineEdit->setText(input);
    }
    catch (const std::bad_alloc &) {input = " ";}
}

QStringList inputArray = input.split('-'); 很重要(或者至少我认为如此),因为我需要将字节拆分成单独的块以便对它们执行各种操作。


举个例子,让大家看看我想做什么样的操作:

char MainWindow::checkSum(QByteArray &b)
{
    char val = 0x00;
    char i;
    foreach (i, b)
    {
       val ^= i;
    }

    return val;
}

如果我理解正确,您将必须 join 带有您选择的 separator 的字符串,然后必须使用 QString 函数之一转换为 QByteArray 取决于 QStrings 编码

QStringList strlist; //Let's assume there's a value in there

QChar separator = ''; //Any QChar or QString you want to use to separate the QStringList
strlist.join(separator).toLocal8Bit(); //8 bit
strlist.join(separator).toUtf8(); //UTF-8
strlist.join(separator).toLatin1(); //Latin

编辑 1 以适合您的代码

if(input.contains("0x")||input.contains(",")||input.contains("-"))
{
    //Just replace the chars you don't cant, then convert back to QByteArray
    input = input.replace("0x","");
    input = input.replace(",", "");
    input = input.replace("-", "");
    QByteArray output = input.toLocal8Bit();
}

用 QStringList 编辑 2

if(input.contains("0x")||input.contains(",")||input.contains("-"))
{
    //Just replace the chars you don't cant, then convert back to QByteArray
    input = input.replace("0x","");
    input = input.replace(",", "");
    QStringList inputArray = input.Split('-');

    //Do what you want with your list
    //...

    //Converts it back
    QByteArray output = inputArray.join('').toLocal8Bit();
}

我现在无法测试代码,所以可能有错字

您只需为列表中的每个字符串应用 QByteArray::append(const QString &str)Docs表示会自动转换字符串:

Appends the string str to this byte array. The Unicode data is converted into 8-bit characters using QString::toUtf8().

所以你可以这样写:

QByteArray output;

// inputArray - your QStringList
foreach (const QString &str, inputArray)
{
    output.append(str);
}

但是如果您不想要默认对话,您可以附加转换为 const char * 的字符串,如同一文档中所述:

You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toUtf8() (or QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to convert the data to const char *.

然后做这样的事情:

output.append(str.toUtf8()); // or str.toLatin1() or str.toLocal8Bit()

编辑 1

刚发现你也可以用+=代替append() :

output += str;

这与 append() 的行为相同 here

用 QDataStream 序列化字符串列表如何?

// Fill the data buffer
QByteArray data;
QStringList list{"foo", "bar", "foobar"};
QDataStream dataStreamWrite(&data, QIODevice::WriteOnly);
dataStreamWrite << list;

// Do whatever you want with the raw data

// And if you want to convert it back to a QStringList
QDataStream dataStreamRead(data);
QStringList list2;
dataStreamRead >> list2;

正如dydil也回答的那样,你可以使用QDataStream

例如:

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

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