Results 1 to 3 of 3

Thread: HTML Unicode ampersand-encoding

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default HTML Unicode ampersand-encoding

    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:

    Qt Code:
    1. QString ampersand_encode(const QString& str){
    2.  
    3. QString chr;
    4.  
    5. for (int i = 0; i < str.size(); ++i) {
    6. chr = QString(str[i]);
    7. list << "&#x" + QString(chr.toUtf8().toHex()) + ";";
    8. }
    9.  
    10. return list.join("");
    11. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Neptilo; 18th December 2012 at 09:03.

Similar Threads

  1. UTF-8 / unicode Japanese encoding problem
    By BonRouge in forum Qt Programming
    Replies: 0
    Last Post: 5th September 2011, 10:02
  2. convert ampersand encoded HTML into something readable
    By tetsuoii in forum Qt Programming
    Replies: 5
    Last Post: 24th October 2010, 18:49
  3. Encoding/decoding a string to URL and HTML
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 25th September 2010, 21:40
  4. Replies: 1
    Last Post: 8th June 2010, 00:40
  5. ampersand showing in QLabel
    By dave in forum Newbie
    Replies: 6
    Last Post: 7th November 2006, 06:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.