URL Decoding

Decodes URL-encoded string



									public static string DecodeURL(string data)
{
	StringBuilder result = new StringBuilder(data.Length);

	for (int i = 0; i < data.Length; ++i)
	{
		if (data[i] == '%')
		{
			result.Append((char)HexadecimalToDecimal(data.Substring(i + 1, 2)));
			i += 2;
		}
		else
		{
			result.Append(data[i]);
		}
	}

	return result.ToString();
}

private static int HexadecimalToDecimal(string hex)
{
	hex = hex.ToUpper();

	int hexLength = hex.Length;
	double dec = 0;

	for (int i = 0; i < hexLength; ++i)
	{
		byte b = (byte)hex[i];

		if (b >= 48 && b <= 57)
			b -= 48;
		else if (b >= 65 && b <= 70)
			b -= 55;

		dec += b * Math.Pow(16, ((hexLength - i) - 1));
	}

	return (int)dec;
}
								


Example

									string data = "jdfgsdhfsdfsd%206445dsfsd7fg%2F%2A%2F%2Bbfjsdgf%25%24%5E";
string value = DecodeURL(data);
								


Output

									jdfgsdhfsdfsd 6445dsfsd7fg/*/+bfjsdgf%$^