使用嵌套编码解码字符串

时间:2012-10-31 11:56:02

标签: c# visual-studio-2010 encoding

strong text给出一个已编码n次的字符串,如果我不知道n的值,如何解码字符串给我简单的未编码字符串。

澄清:

Initial : /unencoded?string/

Encoded Once : %2Funencoded%3Fstring%2F

Encoded Twice: %252Funencoded%253Fstring%252F

Encoded Three Times: %25252Funencoded%25253Fstring%25252F

如何在不知道已编码三次的情况下从%25252Funencoded%25253Fstring%25252F/unencoded?string/

我知道我可以使用HttpServerUtility.UrlDecode或类似的,但这只会解码一次。

2 个答案:

答案 0 :(得分:3)

解码,直到解码不再改变它为止。

string encodedString = "....";
string temp = string.Empty;
string decodedString = HttpServerUtility.UrlDecode(encodedString);
while (decodedString != temp)
{
    temp=decodedString;
    decodedString = HttpServerUtility.UrlDecode(temp);
}

答案 1 :(得分:0)

更新了hometoast的.NET版本答案。

基本上只需与HttpServerUtility.UrlDecode

交换HttpUtility.UrlDecode
string encodedString = "....";
string temp = string.Empty;
string decodedString = HttpUtility.UrlDecode(encodedString);
while (decodedString != temp)
{
    temp=decodedString;
    decodedString = HttpUtility.UrlDecode(temp);
}