user990635
user990635

Reputation: 4249

Converting special characters to regular c#

Is there a command in C# to convert strings like : https%3A%2F%2Fwww.google.com back to https://www.google.com?

some sort of "decryption" method maybe?

Upvotes: 3

Views: 7569

Answers (4)

keyboardP
keyboardP

Reputation: 69362

If you're not working on a web application, I suggest you use the WebUtility class instead as you don't have to import the entire System.Web assembly to access UrlDecode, which is required for the HttpUtility class. (You'll need to be targeting .NET 4)

string unencoded = WebUtility.UrlDecode("https%3A%2F%2Fwww.google.com");

You can also use Uri.UnescapeDataString if don't require any other HTML encoding/decoding methods. This is System.Uri so you don't need to import any other assembly.

Upvotes: 2

Roy Dictus
Roy Dictus

Reputation: 33139

You need to use System.Web.HttpUtility.UrlDecode for this:

string real = System.Web.HttpUtility.UrlDecode(encodedString);

You can use the reverse function System.Web.HttpUtility.UrlEncode to encode.

This is not a matter of encryption or decryption. It is just that some characters cannot be expressed as part of parameters or other in a URL. For instance, a colon (:) cannot be part of a URL tail because it is used in the prefix (http:), so it gets encoded as %3A.

In the same way, a slash gets encoded as %2F. Hence, %3A%2F2%F means ://.

Upvotes: 8

Anirudha
Anirudha

Reputation: 32787

You can try

HttpUtility.UrlDecode(url);

or

Uri.UnescapeDataString(url);

Upvotes: 3

Rik
Rik

Reputation: 29243

You can use HttpUtility.UrlDecode

Upvotes: 4

Related Questions