Dmitry
Dmitry

Reputation: 23

How to convert QByteArray to QString?

Here is HttpServer in JAVA, that sends russian string to Qt application:

 public void handle(HttpExchange t) throws IOException {
        //Response from server in Russian
        String response = "Ответ от сервера на русском языке"; //"Response from server in Russian"
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        //os.write(response.getBytes());
       //right now I am trying this code
      os.write(response.getBytes(Charset.forName("UTF-8"))); //and UTF-16
       //also I have tried this
        os.write(Charset.forName("UTF-16").encode(response).array());

        os.close();
    }

Application in Qt gets response from server (text in russian)

 void MainWindow::Response(QNetworkReply *reply) {
    QByteArray data = reply->readAll();
   // Also I've tried this
   // QString dataString = (QString)reply->readAll();
   // QString dataLine = (QString)reply->readLine();
   // QString str=  QString::fromUtf8(data);
    qDebug() << "data from server: " << data;
     label->setText(str);
} 

I just want to show normal string in console or in label component, but I have empty string or array of bytes or sequence of question marks

"\xCE\xF2\xE2\xE5\xF2 \xEE\xF2 \xF1\xE5\xF0\xE2\xE5\xF0\xE0 \xED\xE0 \xF0\xF3\xF1\xF1\xEA\xEE\xEC \xFF\xE7\xFB\xEA\xE5"

How can I solve this problem with encoding and convert this array to QString and finally see the normal text ?

Upvotes: 2

Views: 9833

Answers (1)

Matteo Italia
Matteo Italia

Reputation: 126917

getBytes

public byte[] getBytes()

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

Using this method to serialize the string is a terrible idea; the "platform default encoding" depends from current locale, operating system and whatnot, while you want a well-defined encoding to transmit data between applications.

In facts, I trust you when you say "I've tried everything, nothing works" - that's because your string is encoded in the windows-1251 encoding, which probably happens to be the default 8-bit encoding of your Windows machine. When you try to decode it on the Qt side - which, from Qt5, always expects UTF-8 by default when decoding 8-bit strings, everything breaks.


First of all, on the Java side encode your string in UTF8, making sure to specify the response length correctly (you have to provide the number of bytes you are writing, not of characters in the source string):

String response = "Ответ от сервера на русском языке"; //"Response from server in Russian"
byte[] response_data = response.getBytes("UTF-8");
t.sendResponseHeaders(200, response_data.length());
OutputStream os = t.getResponseBody();
os.write(response_data);
os.close();

Then, on the Qt side, you can build a QString from the QByteArray, specifying it's in UTF8.

QByteArray data = reply->readAll();
QString str = QString::fromUtf8(data);

(notice that you may send your string in UTF-16 or UTF-32 as well, but of course the two sides must match; I chose UTF-8 because it's way more common as an "on the wire" format and because it's generally more compact)

Upvotes: 3

Related Questions