本文主要是介绍C# Google 翻译TKK计算tk 百度翻译gtk计算sign 通用算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原JS代码
function b(a, b) {for (var d = 0; d < b.length - 2; d += 3) {var c = b.charAt(d + 2),c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),c = "+" == b.charAt(d + 1) ? a >>> c : a << c,a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c}return a
}function tk(a,TKK) {for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {var c = a.charCodeAt(f);128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?(c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)}a = h;for (d = 0; d < g.length; d++) a += g[d],a = b(a, "+-a^+6");a = b(a, "+-3^+b+-f");a ^= Number(e[1]) || 0;0 > a && (a = (a & 2147483647) + 2147483648);a %= 1E6;return a.toString() + "." + (a ^ h)
}
C#代码
工具类
public static class tools
{//实现js的charAt方法public static char charAt(this object obj, int index){char[] chars= obj.ToString().ToCharArray();return chars[index];}//实现js的charCodeAt方法public static int charCodeAt(this object obj, int index){char[] chars= obj.ToString().ToCharArray();return (int)chars[index];}//实现js的Number方法public static int Number(object cc){try{long a= Convert.ToInt64(cc.ToString());int b = a > 2147483647 ? (int)(a - 4294967296) : a < -2147483647 ? (int)(a + 4294967296) : (int)a;return b;}catch (Exception){return 0;}}
}
完整C#代码
public static string b(long a, string b)
{for (int d = 0; d < b.Length - 2; d += 3){char c = b.charAt(d + 2);int c0 = 'a' <= c ? c.charCodeAt(0) - 87 : tools.Number(c);long c1 = '+' == b.charAt(d + 1) ? a >> c0 : a << c0;a = '+' == b.charAt(d) ? a + c1 & 4294967295 : a ^ c1;}a = tools.Number(a);return a.ToString();
}public static string tk(string a, string TKK)
{string[] e = TKK.Split('.');int d = 0;int h = 0;h = Number(e[0]);byte[] g0 = Encoding.UTF8.GetBytes(a);long aa = h;for (d = 0; d < g0.Length; d++){aa += g0[d];aa = Convert.ToInt64(b(aa, "+-a^+6"));}aa = Convert.ToInt64(b(aa, "+-3^+b+-f"));long bb = aa ^ Number(e[1]);aa = bb;aa = aa + bb;bb = aa - bb;aa = aa - bb;if (0 > aa){aa = (aa & 2147483647) + 2147483648;}aa %= (long)1e6;return aa.ToString() + "." + (aa ^ h);
}
csdn下载地址(Google翻译C#版demo 可运行)
这篇关于C# Google 翻译TKK计算tk 百度翻译gtk计算sign 通用算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!