QByteArray to QString
Asked Answered
S

7

55

I'm having issues with QByteArray and 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

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.

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". How can I fix it?

Span answered 2/1, 2013 at 23:55 Comment(2)
Possibly useful: qt-project.org/doc/qt-4.8/…Subsidize
Please note that string handling changed since Qt 5.0. Character arrays are internally interpreted as being UTF-8 encoded. QString myValue = "äöü"; for example should be avoided at least if the source file it self is not stored in UTF-8. Take a look at wiki.qt.io/Strings_and_encodings_in_Qt for more information.Winnebago
S
30

You can use QTextCodec to convert the bytearray to a string:

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

(1015 is 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).

Sauerbraten answered 3/1, 2013 at 0:5 Comment(5)
Not sure why this is a so upvoted reply. This is giving an unusual solution that the majority uses and should use. Providing it alongside the usual conversion methods would have been a good reply, but on its own, it is strange.Meijer
@LaszloPapp: Do you mean QString::fromUTf16? That takes a ushort pointer, which would require some very ugly casting to pass a QByteArraySauerbraten
@BeniBela: yes, but why cannot it be utf8? In which case why not suggest from/toUtf8()?Meijer
@LaszloPapp: Because of the example. He wants to convert t\0e\0s\0t\0 to test. If it were utf-8, there would be no \0-bytesSauerbraten
@BeniBela: OK, fair enough. Can you make this clear in your answer why you suggest utf16? As you can see, it was a bit unclear for me. You will also get a few more upvotes with the bumping of the thread. :)Meijer
A
50

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

QString(const QByteArray &ba)

QByteArray data;
QString DataAsString = QString(data);
Ardell answered 11/1, 2019 at 23:7 Comment(1)
This should be the accepted answer. Functional and simpler (let Qt handle conversion automatically)Custodian
S
30

You can use QTextCodec to convert the bytearray to a string:

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

(1015 is 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).

Sauerbraten answered 3/1, 2013 at 0:5 Comment(5)
Not sure why this is a so upvoted reply. This is giving an unusual solution that the majority uses and should use. Providing it alongside the usual conversion methods would have been a good reply, but on its own, it is strange.Meijer
@LaszloPapp: Do you mean QString::fromUTf16? That takes a ushort pointer, which would require some very ugly casting to pass a QByteArraySauerbraten
@BeniBela: yes, but why cannot it be utf8? In which case why not suggest from/toUtf8()?Meijer
@LaszloPapp: Because of the example. He wants to convert t\0e\0s\0t\0 to test. If it were utf-8, there would be no \0-bytesSauerbraten
@BeniBela: OK, fair enough. Can you make this clear in your answer why you suggest utf16? As you can see, it was a bit unclear for me. You will also get a few more upvotes with the bumping of the thread. :)Meijer
I
28

You can use:

QString::fromStdString(byteArray.toStdString())
Insulator answered 8/12, 2017 at 11:54 Comment(0)
T
14

you can use QString::fromAscii()

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

with data() returning a 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

Thorncombe answered 13/11, 2013 at 13:25 Comment(4)
*Ascii() is deprecated in Qt 5! Do not use it.Meijer
Actually he can not use any of this, because he wants to convert utf-16, not asciiSauerbraten
I can't seem to find fromCString(), did you mean (or was the method renamed since the Q / A) to fromCFString() ?Narbonne
No actually use fromLatin1 instead of fromAsciiKilan
S
10

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

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

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

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

Sinaloa answered 22/7, 2016 at 15:59 Comment(1)
Although he needs fromUtf16, I like this answer too. Tidy yet specific.Louiselouisette
R
6

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

#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. The code has been tested using Qt 5.4.

Rainstorm answered 28/5, 2015 at 1:0 Comment(0)
N
1

Qt 5.12 and up:

QString::fromStdString(byteArray.toStdString());
Nonstandard answered 4/3, 2020 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.