Results 1 to 3 of 3

Thread: HTML Unicode ampersand-encoding

Hybrid 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 10:03.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HTML Unicode ampersand-encoding

    How about this?

    Qt Code:
    1. QString ampersand_encode(const QString &string) {
    2. QString encoded;
    3. for(int i=0;i<string.size();++i) {
    4. QChar ch = string.at(i);
    5. if(ch.unicode() > 255)
    6. encoded += QString("&#%1;").arg((int)ch.unicode());
    7. else
    8. encoded += ch;
    9. }
    10. return encoded;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Neptilo (19th December 2012)

  4. #3
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: HTML Unicode ampersand-encoding

    Thank you! It works!

    Actually I realized my tests were wrong. I entered text directly in my code and Unicode characters were split in 8-bit characters. Now that I enter text in a QLineEdit, it works much better.

Similar Threads

  1. UTF-8 / unicode Japanese encoding problem
    By BonRouge in forum Qt Programming
    Replies: 0
    Last Post: 5th September 2011, 11:02
  2. convert ampersand encoded HTML into something readable
    By tetsuoii in forum Qt Programming
    Replies: 5
    Last Post: 24th October 2010, 19:49
  3. Encoding/decoding a string to URL and HTML
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 25th September 2010, 22:40
  4. Replies: 1
    Last Post: 8th June 2010, 01:40
  5. ampersand showing in QLabel
    By dave in forum Newbie
    Replies: 6
    Last Post: 7th November 2006, 07: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.