How unescape RFC 3986 string

395 Views Asked by At

I've a RFC 3986 encoded string in the form %x##. For example the space character is encoded as %x20 instead of %20. How can I decode it in C#? Using the decode method of Uri, HttpUtility or WebUtility classes the string was not decoded.

2

There are 2 best solutions below

2
Dmitry Bychenko On BEST ANSWER

You can try regular expressions in order to Replace all %x## as well as %## matches:

  using System.Text.RegularExpressions;

  ...

  string demo = "abc%x20def%20pqr";

  string result = Regex.Replace(
      demo, 
    "%x?([0-9A-F]{2})", 
      m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(), 
      RegexOptions.IgnoreCase);

  Console.Write(result);

Outcome:

  abc def pqr
0
Gauravsa On

You can try something like this: You can try:

Reference: How to get Uri.EscapeDataString to comply with RFC 3986

    var escapedString = new StringBuilder(Uri.EscapeDataString(value));

    for (int i = 0; i < UriRfc3986CharsToEscape.Length; i++) {
        escapedString.Replace(UriRfc3986CharsToEscape[i], Uri.HexEscape(UriRfc3986CharsToEscape[i][0]));
    }

    // Return the fully-RFC3986-escaped string.
    return escaped.ToString();