1

I want to convert a part of QUrl to Unicode.

I have a given URL, www.ÄSDF.de and want to convert it to www.%C4SDF.de. How can I do that in Qt?

When I use the method QUrl::toEncoded() I always get the converted URL in UTF-8 hex: "www.%C3%83%C2%84SDF.de".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Garrz
  • 19
  • 1
  • 2
  • With - QUrl::toPercentEncoding(QString::fromUtf8("www.ÄSDF.de")); - i get now the right utf-8 code "www.%C3%84SDF.de", but how can i convert this URL in unicode "www.%C4SDF.de"? – Garrz Aug 24 '11 at 09:15

2 Answers2

4

You can't generate www.%C4SDF.de with QUrl::toPercentEncoding as that function always encodes to UTF-8 byte sequences before %-encoding.

If you really must use a non-UTF-8 encoding like ISO-8859-1 (typically for compatibility with unfortunate legacy applications), you will have to use QByteArray::toPercentEncoding on a byte array you generate from QString::toLatin1.

However, you probably don't want to do that either. Even the UTF-8-correct www.%C3%84SDF.de is not a valid way of specifying the hostname www.ÄSDF.de in a URI. Instead it must be encoded using the IDN algorithm (using Punycode), giving www.xn--sdf-pla.de.

The easy and usually best way to proceed would be QUrl::toEncoded. This turns an IRI, eg:

http://www.äsdf.de/äsdf?äsdf=äsdf

into a working URI:

http://www.xn--sdf-pla.de/%C3%A4sdf?%C3%A4sdf

(note again IRI requires UTF-8.)

bobince
  • 528,062
  • 107
  • 651
  • 834
0

toEncodedUrl has been deprecated in QT5.
I did this:
url.setUrl(QString(QUrl::toPercentEncoding(s, "/:")));