C++ QByteArray 到 QString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14131127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:01:21  来源:igfitidea点击:

QByteArray to QString

c++qtqstringqtcoreqbytearray

提问by Nika

I'm having issues with QByteArrayand QString.

我遇到了QByteArray和问题QString

I'm reading a file and stores its information in a QByteArray. The file is in unicode, so it contains something like: t\0 e\0 s\0 t\0 \0 \0

我正在读取一个文件并将其信息存储在QByteArray. 该文件是 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
QString DataAsString = QTextCodec::codecForMib(1015)->toUnicode(Data);
e
QString::fromStdString(byteArray.toStdString())
s
QByteArray data;
QString DataAsString = QString(data);
t
QByteArray data = entity->getData();
QString s_data = QString::fromAscii(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', '
QString(byteArray).toStdString();
', 'e', '
byteArray.toStdString();
', 's', '##代码##', 't', '##代码##', '##代码##', '##代码##'}; 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(); }
##代码##" 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 Datahas the value "t\0 e\0 s\0 t\0 \0 \0"and myValuehas the value "test". How can I fix it?

在调试器中,它向我显示该变量Data具有 value"t\0 e\0 s\0 t\0 \0 \0"并且myValue具有 value "test"。我该如何解决?

采纳答案by BeniBela

You can use QTextCodecto convert the bytearray to a string:

您可以使用QTextCodec将 bytearray 转换为字符串:

##代码##

(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, i.e. every ascii character is followed by a \0-byte, or resp. every ascii character is encoded as 2 bytes. 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).

从您的示例中,我们可以看到字符串"test"的编码方式 "t\0 e\0 s\0 t\0 \0 \0"与您的编码一样,即每个 ascii 字符后跟一个\0-byte 或 resp。每个 ascii 字符都编码为 2 个字节。以这种方式编码 ascii 字母的唯一 unicode 编码是 UTF-16 或 UCS-2(这是 UTF-16 的受限版本),因此在您的情况下需要 1015 mib(假设您的本地字节序是与输入字节序相同)。

回答by Tarek.Mh

You can use:

您可以使用:

##代码##

回答by Link H.

You can use this QString constructor for conversion from QByteArray to QString:

您可以使用此 QString 构造函数将 QByteArray 转换为 QString:

QString(const QByteArray &ba)

QString(const QByteArray &ba)

##代码##

回答by kiriloff

you can use QString::fromAscii()

您可以使用 QString::fromAscii()

##代码##

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-21872https://bugreports.qt.io/browse/QTBUG-21872

对于 QT5,您应该fromCString()改用,因为fromAscii()已弃用,请参阅https://bugreports.qt-project.org/browse/QTBUG-21872https://bugreports.qt.io/browse/QTBUG-21872

回答by Efreeto

You may find QString::fromUtf8()also useful.

您可能会发现QString::fromUtf8()也很有用。

For QByteArray inputof "\010"and "\000",

对于QByteArray input"\010""\000"

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"

回答by jonathanzh

Use QString::fromUtf16((ushort *)Data.data()), as shown in the following code example:

使用QString::fromUtf16((ushort *)Data.data()),如以下代码示例所示:

##代码##

This is an alternative solution to the one using QTextCodec. The code has been tested using Qt 5.4.

这是使用 QTextCodec 的替代解决方案。该代码已使用 Qt 5.4 进行了测试。

回答by Роман Худобердин

Qt 4.8

Qt 4.8

##代码##

Qt 5.12

Qt 5.12

##代码##