关于punycode解码的问题
目前有一些经过punycode加码后无法正常解码的字符串但是可以一些解码网站提供的punycode解码服务得到解码,很不解?
实例字符串:xn--fiQ31H22EepUpwBd74Dlp9A.cn
经过以下程序无法正常解码:
public static String decode(String input)
{
int n = INITIAL_N;
int i = 0;
int bias = INITIAL_BIAS;
StringBuilder output = new StringBuilder();
int d = input.LastIndexOf(DELIMITER);
if (d > 0)
{
for (int j = 0; j < d; j++)
{
char c = input[j];
if (!isBasic(c))
{
throw new Exception("BAD_INPUT");
}
output.Append(c);
}
d++;
}
else
{
d = 0;
}
while (d < input.Length)
{
int oldi = i;
int w = 1;
for (int k = BASE; ; k += BASE)
{
if (d == input.Length)
{
throw new Exception("BAD_INPUT");
}
int c = input[d++];
int digit = codepoint2digit(c);
if (digit > (int.MaxValue - i) / w)
{
throw new Exception("OVERFLOW");
}
i = i + digit * w;
int t;
if (k <= bias)
{
t = TMIN;
}
else if (k >= bias + TMAX)
{
t = TMAX;
}
else
{
t = k - bias;
}
if (digit < t)
{
break;
}
w = w * (BASE - t);
}
bias = adapt(i - oldi, output.Length + 1, oldi == 0);
if (i / (output.Length + 1) > int.MaxValue - n)
{
throw new Exception("OVERFLOW");
}
n = n + i / (output.Length + 1);
i = i % (output.Length + 1);
output.Insert(i, (char)n);
i++;
}
return output.ToString();
}
public static int adapt(int delta, int numpoints, bool first)
{
if (first)
{
delta = delta / DAMP;
}
else
{
delta = delta / 2;
}
delta = delta + (delta / numpoints);
int k = 0;
while (delta > ((BASE - TMIN) * TMAX) / 2)
{
delta = delta / (BASE - TMIN);
k = k + BASE;
}
return k + ((BASE - TMIN + 1) * delta) / (delta + SKEW);
}
public static bool isBasic(char c)
{
return c < 0x80;
}
public static int codepoint2digit(int c)
{
if (c - '0' < 10)
{
// '0'..'9' : 26..35
return c - '0' + 26;
}
else if (c - 'a' < 26)
{
// 'a'..'z' : 0..25
return c - 'a';
}
else
{
throw new Exception("BAD_INPUT");
}
}
}
但是在随便一个提供punycode解码的网站都可以正常解码。例如:http://www.nicenic.com/domain/punycode.php 并选择转成GBK中文,然后点击转换。成功转换并显示结果。
兄弟们看看哪里有问题。