7

In QT 5.4 and C++ I try to decode a string that has unicode entities.

I have this QString:

QString string = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";

I want to convert this string to this: fileÖÇŞİĞÜİçöşiğüı.txt

I tried QString's toUtf8 and fromUtf8 methods. Also tried to decode it character by character.

Is there a way to convert it by using Qt?

trante
  • 33,518
  • 47
  • 192
  • 272
  • 1
    Probably your console is not configured to print Unicode. If you are executing on Windows Prompt, try this http://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app – gibertoni Apr 09 '15 at 21:39
  • Thank you. Yes windows console also has unicode problems, but I need to decode it in my cpp codes. – trante Apr 10 '15 at 07:09
  • Your string literal contains Unicode escapes, characters which probably won't fit in 8 bits. And on al Qt platforms, `char` is 8 bits. You'll probably need a wide literal: `L"file\u00d6\u00c7\u015e\u0130..."` – MSalters Apr 10 '15 at 07:40
  • offtopic: for me this string looks like something what should be a application configuration setting and not hard-coded. – Marek R Apr 15 '15 at 07:27

3 Answers3

7

Qt provides a macro called QStringLiteral for handling string literals correctly.

Here's a full working example:

#include <QString>
#include <QDebug>

int main(void) {
   QString string = QStringLiteral("file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt");
   qDebug() << string;

   return 0;
}

As mentioned in the above comments, you do need to print to a console that supports these characters for this to work.

MrEricSir
  • 8,044
  • 4
  • 30
  • 35
  • This works for text arguments. When I pass a QString as argument, this doesn't work. ```QString string1 = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";``` ```QString string2 = QStringLiteral(string);``` – trante Apr 14 '15 at 10:38
  • 1
    As the name suggests, it's *only* intended for string literals. So in your example you should have wrapped yours like so: `QString string1 = QStringLiteral("file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u‌​0131.txt");` – MrEricSir Apr 14 '15 at 16:26
2

I have just tested this code:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString s = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
    qDebug() << s.length();  //Outputs: 22
    qDebug() << s;           //Outputs: fileÖÇŞİĞÜİçöşiğüı.txt
    return a.exec();
}

This is with Qt 5.4 on ubuntu, so it looks like your problem is with some OS only.

Marco
  • 1,952
  • 1
  • 17
  • 23
0
#include <QTextDocument>

QTextDocument doc;

QString string = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
doc.setHtml(string);                   // to convert entities to text
QString result = doc.toPlainText();    // result = "fileÖÇŞİĞÜİçöşiğüı.txt"

NOT USEFUL if you have a CONSOLE app QTextDocument needs the GUI module.