我有一个Javascript方法。我在C#上写过,但它不起作用。
Javascript代码
var __AM = 65521;
function cc(a) {
var c = 1, b = 0, d, e;
for (e = 0; e < a.length; e++) {
d = a.charCodeAt(e);
c = (c + d) % __AM;
b = (b + c) % __AM;
}
return b << 16 | c;
}
我写的C#代码
private string CC(string a)
{
var __AM = 65521;
int e;
long d;
long c = 1, b = 0;
for (e = 0; e < a.Length; e++)
{
var p = Encoding.Unicode.GetBytes(a[e].ToString());
d = Convert.ToInt32(p.First());
c = (c + d) % __AM;
b = (b + c) % __AM;
}
return b.ToString() + c.ToString();
}
JS测试
cc("4JipHEz53sU1406413803");
结果:1132332429
C#测试
CC("4JipHEz53sU1406413803");
结果:172781421
如何在C#中获取JS值?
答案 0 :(得分:2)
此代码有效:
private string cc(string a)
{
var __AM = 65521;
int e;
long d;
long c = 1, b = 0;
for (e = 0; e < a.Length; e++)
{
d = (int)a[e];
c = (c + d) % __AM;
b = (b + c) % __AM;
}
return (b << 16 | c).ToString();
}