Advertisement
cansik

Decode URL

Feb 10th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. /// <summary>
  2. /// Decodes an encoded URL
  3. /// </summary>
  4. /// <param name="str">Input URL</param>
  5. /// <returns>decoded URL</returns>
  6. public static string Decode(string str)
  7. {
  8.     Regex hexReg = new Regex(@"%(?<value>[A-E\d]{2})");
  9.     StringBuilder b = new StringBuilder(str);
  10.  
  11.     int offset = 0;
  12.     MatchCollection matches = hexReg.Matches(str);
  13.  
  14.     foreach (Match m in matches)
  15.     {
  16.         string hexValue = m.Groups[1].Value;
  17.         int decValue = Convert.ToInt32(hexValue, 16);
  18.         string charValue = Char.ConvertFromUtf32(decValue);
  19.  
  20.         b.Remove(m.Index - offset, m.Length);
  21.         b.Insert(m.Index - offset, charValue);
  22.  
  23.         offset += m.Length - charValue.Length;
  24.     }
  25.  
  26.     return b.ToString();
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement