Results 1 to 3 of 3

Thread: Convert to html percent encoding

  1. #1
    Join Date
    Oct 2016
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Convert to html percent encoding

    Hi, I have what seems a simple issue but I was unable to solve.

    I'm writing an application that login to an internet site. The problem is that the password contains a special character, À, but login fails. After a while I discovered that the problem is how qt converts the letter; instead of "%C0" as in every URL Encoded Characters Table, every qt method I used converts it in "%C3%80".

    I tryed QByteArray::toPercentEncoding(), QUrl::toEncoded((FormattingOptions), Qurl::toString((FormattingOptions), without luck.

    I'm using qt 5.7 on arch linux.

    Thanks for your help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,553
    Thanks
    13
    Thanked 1,580 Times in 1,509 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Convert to html percent encoding

    Your QString is a collection of Unicode character codes. The character 'À' is Unicode code point U+00C0. The UTF-8 encoding for that code point is two bytes 0xC3 0x80. I think you can see where your result is coming from. This behaviour is exactly as documented http://doc.qt.io/qt-5/qurl.html#toPercentEncoding

    The original RFC talks about encoding the ASCII value of reserved characters: there is no ASCII code for 'À'. RFC 3986, in 2005, changed this to UTF-8, which explains the Qt behaviour.

    Your server is expecting that the character has been encoded with a local eight bit encoding (probably Windows CP1252) where that character would be a single byte 0xC0. It is possible the server triggers UTF-8 handling if the request has an Accept-Charset header listing utf-8, so that is worth a try. If not, you may have to manually encode the bytes out of QString:toLocal8Bit() to get the result your server expects.

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

    bull3t (31st October 2016)

  4. #3
    Join Date
    Oct 2016
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Convert to html percent encoding

    Thanks for tips. Unfortunately "Accept-Charset" has no effect on server. Also QString:toLocal8Bit() seems not working; here a small piece of code:

    Qt Code:
    1. QString pass = "À";
    2. QUrlQuery postData;
    3. postData.addQueryItem( QLatin1String( "pass" ), pass.toLocal8Bit() );
    4. qDebug() << postData.query( QUrl::EncodeUnicode );
    To copy to clipboard, switch view to plain text mode 

    output is "pass=c%C3%80"

    EDIT 1:

    postData.query( QUrl::PrettyDecoded ) returns

    "pass=\xC0"

    that's very close to "%C0"

    EDIT 2:

    The following code seems working:

    Qt Code:
    1. QUrl(pass).toString(QUrl::PrettyDecoded).toLatin1().toPercentEncoding()
    To copy to clipboard, switch view to plain text mode 
    Last edited by bull3t; 31st October 2016 at 19:23.

Similar Threads

  1. QUrl Percent Encoding and addQuery
    By ttimt in forum Newbie
    Replies: 3
    Last Post: 31st March 2015, 18:16
  2. HTML Unicode ampersand-encoding
    By Neptilo in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2012, 23:01
  3. Convert html to bbcode
    By abrahametalero in forum Newbie
    Replies: 1
    Last Post: 2nd March 2011, 23:14
  4. Encoding/decoding a string to URL and HTML
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 25th September 2010, 22:40
  5. How to convert text to HTML in QTextEdit
    By Roszko in forum Newbie
    Replies: 5
    Last Post: 31st December 2009, 10:40

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.