有关java代码翻译成javascript的问题?

whiteclude 2002-06-19 05:32:38
有这么一段java代码:
public static String decode(String s)
StringBuffer sb = new StringBuffer();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch(c) {
case 37: // '%'
try {
sb.append((char)Integer.parseInt(s.substring(i + 1, i + 3), 16));
}
catch(NumberFormatException _ex)
{
throw new IllegalArgumentException();
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
String s1 = sb.toString();
byte abyte0[] = s1.getBytes("8859_1");
return new String(abyte0);
}

谁有能力把他翻译成javascript的函数.一定200分相赠.

正确的翻译后应该可以把 "%B9%A7%CF%B2%C4%E3%B7%AD%D2%EB%B3%F6%D5%FD%C8%B7%B5%C4%D7%D6%B7%FB%B4%AE" 翻译成 "恭喜你翻译出正确的字符串"
...全文
103 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
javascript不行,因为走unicode

<script language=vbs>
function urldecoding(vstrin)
'qiushuiwuhen(2002-6-19)
dim i,strreturn,strSpecial
strSpecial = " <>""#%{}|^[]`&?+"
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if thischr="%" then
intasc=eval("&h"+mid(vstrin,i+1,2))
if instr(strSpecial,chr(intasc))>0 then
strreturn= strreturn & chr(intasc)
i=i+2
else
intasc=eval("&h"+mid(vstrin,i+1,2)+mid(vstrin,i+4,2))
strreturn= strreturn & chr(intasc)
i=i+5
end if
else
strreturn= strreturn & thischr
end if
next
urldecoding = strreturn
end function
function urlencoding(vstrin)
dim i,strreturn,strSpecial
strSpecial = " <>""#%{}|^[]`&?+"
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if abs(asc(thischr)) < &hff then
if instr(strSpecial,thischr)>0 then
strreturn = strreturn & "%" & hex(asc(thischr))
else
strreturn = strreturn & thischr
end if
else
innercode = asc(thischr)
if innercode < 0 then
innercode = innercode + &h10000
end if
hight8 = (innercode and &hff00)\ &hff
low8 = innercode and &hff
strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
end if
next
urlencoding = strreturn
end function
str="恭喜你翻译出正确的字符串"
strEncode=urlencoding(str)
strDecode=urldecoding(strEncode)
document.write str,"=>",strEncode,"=>",strDecode
</script>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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