URL Decoding

Decodes URL-encoded string



									Public Shared Function DecodeURL(data As String) As String
	Dim result As New StringBuilder(data.Length)

	For i As Integer = 0 To data.Length - 1
		If data(i) = "%"c Then
			result.Append(ChrW(HexadecimalToDecimal(data.Substring(i + 1, 2))))
			i += 2
		Else
			result.Append(data(i))
		End If
	Next

	Return result.ToString()
End Function

Private Shared Function HexadecimalToDecimal(hex As String) As Integer
	hex = hex.ToUpper()

	Dim hexLength As Integer = hex.Length
	Dim dec As Double = 0

	For i As Integer = 0 To hexLength - 1
		Dim b As Byte = CByte(AscW(hex(i)))

		If b >= 48 AndAlso b <= 57 Then
			b -= 48
		ElseIf b >= 65 AndAlso b <= 70 Then
			b -= 55
		End If

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

	Return CInt(Math.Truncate(dec))
End Function
								


Example

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


Output

									jdfgsdhfsdfsd 6445dsfsd7fg/*/+bfjsdgf%$^