简体   繁体   English

QByteArray 到 QString

[英]QByteArray to QString

I'm having issues with QByteArray and QString .我在使用QByteArrayQString遇到问题。

I'm reading a file and stores its information in a QByteArray .我正在读取文件并将其信息存储在QByteArray The file is in unicode, so it contains something like: t\\0 e\\0 s\\0 t\\0 \\0 \\0该文件是 unicode,所以它包含如下内容: t\\0 e\\0 s\\0 t\\0 \\0 \\0

I'm trying to compare this value to my specified value, but it fails, because in the debugger I see it's not an unicode string.我试图将此值与我指定的值进行比较,但它失败了,因为在调试器中我看到它不是一个 unicode 字符串。

The code will explain everything:代码将解释一切:

QByteArray Data; //contains unicode string "t\0 e\0 s\0 t\0 \0 \0"
QString myValue = "test"; //value to compare.
if(Data.contains(myValue))
    //do some stuff.
else
    //do other stuff.

In the debugger, it shows me that the variable Data has the value "t\\0 e\\0 s\\0 t\\0 \\0 \\0" and myValue has the value "test" .在调试器中,它显示变量Data的值为"t\\0 e\\0 s\\0 t\\0 \\0 \\0"myValue的值为"test" How can I fix it?我该如何解决?

You can use this QString constructor for conversion from QByteArray to QString:您可以使用此 QString 构造函数将 QByteArray 转换为 QString:

QString(const QByteArray &ba) QString(const QByteArray &ba)

QByteArray data;
QString DataAsString = QString(data);

You can use QTextCodec to convert the bytearray to a string:您可以使用QTextCodec将 bytearray 转换为字符串:

QString DataAsString = QTextCodec::codecForMib(1015)->toUnicode(Data);

(1015 is UTF-16, 1014 UTF-16LE, 1013 UTF-16BE, 106 UTF-8) (1015 是 UTF-16,1014 是 UTF-16LE,1013 是 UTF-16BE,106 是 UTF-8)

From your example we can see that the string "test" is encoded as "t\\0 e\\0 s\\0 t\\0 \\0 \\0" in your encoding, ie every ascii character is followed by a \\0 -byte, or resp.从您的示例中,我们可以看到字符串"test"在您的编码中被编码为"t\\0 e\\0 s\\0 t\\0 \\0 \\0" ,即每个 ascii 字符后跟一个\\0字节,或every ascii character is encoded as 2 bytes.每个 ascii 字符都编码为 2 个字节。 The only unicode encoding in which ascii letters are encoded in this way, are UTF-16 or UCS-2 (which is a restricted version of UTF-16), so in your case the 1015 mib is needed (assuming your local endianess is the same as the input endianess).以这种方式编码 ascii 字母的唯一 unicode 编码是 UTF-16 或 UCS-2(这是 UTF-16 的受限版本),因此在您的情况下需要 1015 mib(假设您的本地字节序是与输入字节序相同)。

您可以使用:

QString::fromStdString(byteArray.toStdString())

you can use QString::fromAscii()你可以使用QString::fromAscii()

QByteArray data = entity->getData();
QString s_data = QString::fromAscii(data.data());

with data() returning a char* data()返回一个char*

for QT5, you should use fromCString() instead, as fromAscii() is deprecated, see https://bugreports.qt-project.org/browse/QTBUG-21872 https://bugreports.qt.io/browse/QTBUG-21872对于 QT5,您应该使用fromCString()代替,因为fromAscii()已弃用,请参阅 https://bugreports.qt-project.org/browse/QTBUG-21872 https://bugreports.qt.io/browse/QTBUG- 21872

You may find QString::fromUtf8() also useful.您可能会发现QString::fromUtf8()也很有用。

For QByteArray input of "\\010" and "\\000" ,对于"\\010""\\000" QByteArray input

QString::fromLocal8Bit(input, 1) returns "\\010" and "" QString::fromLocal8Bit(input, 1)返回"\\010"""

QString::fromUtf8(input, 1) correctly returns "\\010" and "\\000" . QString::fromUtf8(input, 1)正确返回"\\010""\\000"

Use QString::fromUtf16((ushort *)Data.data()) , as shown in the following code example:使用QString::fromUtf16((ushort *)Data.data()) ,如以下代码示例所示:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // QByteArray to QString
    // =====================

    const char c_test[10] = {'t', '\0', 'e', '\0', 's', '\0', 't', '\0', '\0', '\0'};
    QByteArray qba_test(QByteArray::fromRawData(c_test, 10));
    qDebug().nospace().noquote() << "qba_test[" << qba_test << "]"; // Should see: qba_test[t

    QString qstr_test = QString::fromUtf16((ushort *)qba_test.data());
    qDebug().nospace().noquote() << "qstr_test[" << qstr_test << "]"; // Should see: qstr_test[test]

    return a.exec();
}

This is an alternative solution to the one using QTextCodec.这是使用 QTextCodec 的替代解决方案。 The code has been tested using Qt 5.4.该代码已使用 Qt 5.4 进行了测试。

QString a = "test";
QByteArray a1 = a.toUtf8();
QString backToString = a1;

Qt 4.8 Qt 4.8

QString(byteArray).toStdString();

Qt 5.12 Qt 5.12

byteArray.toStdString();

You can use: 您可以使用:

QString DataAsString = data.trimmed();

Data is a QByteArray. 数据是一个QByteArray。

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

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