Neptilo
18th December 2012, 08:58
I would like to convert a QString containing Unicode characters to plain HTML text. For instance "ç§" would become "&" followed by "#31169;" (couldn't display it in a single string).
Is there such a functionality in Qt? I found the function QString Qt::escape ( const QString & plain ) but it only converts HTML metacharacters <, >, &, and ".
Waiting for a better way, I tried to write my own encoding function:
QString ampersand_encode(const QString& str){
QString chr;
QStringList list = QStringList();
for (int i = 0; i < str.size(); ++i) {
chr = QString(str[i]);
list << "&#x" + QString(chr.toUtf8().toHex()) + ";";
}
return list.join("");
}
It almost works. It works for ASCII characters but when I try it with other Unicode characters I only get Korean characters. Why? I feel there's not much to change but I don't know what. Also, any improvement of my code would be appreciated. :)
Is there such a functionality in Qt? I found the function QString Qt::escape ( const QString & plain ) but it only converts HTML metacharacters <, >, &, and ".
Waiting for a better way, I tried to write my own encoding function:
QString ampersand_encode(const QString& str){
QString chr;
QStringList list = QStringList();
for (int i = 0; i < str.size(); ++i) {
chr = QString(str[i]);
list << "&#x" + QString(chr.toUtf8().toHex()) + ";";
}
return list.join("");
}
It almost works. It works for ASCII characters but when I try it with other Unicode characters I only get Korean characters. Why? I feel there's not much to change but I don't know what. Also, any improvement of my code would be appreciated. :)