关于punycode解码的问题

jackymi 2009-08-06 08:00:01
目前有一些经过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中文,然后点击转换。成功转换并显示结果。


兄弟们看看哪里有问题。
...全文
302 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangyanyang 2009-08-07
  • 打赏
  • 举报
回复
帮顶
jackymi 2009-08-07
  • 打赏
  • 举报
回复
楼上的兄弟,你看清楚我的题目没有?
bigmingming 2009-08-07
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20060726/09/4904927.html
jackymi 2009-08-07
  • 打赏
  • 举报
回复
楼上什么意思
Wilson伟庭 2009-08-06
  • 打赏
  • 举报
回复
樓上又灌水嫌疑~,而且還打廣告~

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧