Garrz
Garrz

Reputation: 19

Convert QString or QUrl to Unicode

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".

Upvotes: 1

Views: 6587

Answers (2)

user3915158
user3915158

Reputation: 1

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

Upvotes: 0

bobince
bobince

Reputation: 536379

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.)

Upvotes: 4

Related Questions