Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt convert unicode entities

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?

like image 814
trante Avatar asked Apr 07 '15 06:04

trante


2 Answers

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.

like image 186
MrEricSir Avatar answered Nov 17 '22 11:11

MrEricSir


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.

like image 33
Marco Avatar answered Nov 17 '22 13:11

Marco