JAVA 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)
}

JAVA代码

工具类
public class tools {
	// 实现js的charAt方法
	public static char charAt(Object obj, int index) {
		char[] chars = obj.toString().toCharArray();
		return chars[index];
	}

	// 实现js的charCodeAt方法
	public static int charCodeAt(Object obj, int index) {
		char[] chars = obj.toString().toCharArray();
		return (int) chars[index];
	}

	// 实现js的Number方法
	public static int Number(Object cc) {
		try {
			long a = Long.parseLong(cc.toString());
			int b = a > 2147483647 ? (int) (a - 4294967296L) : a < -2147483647 ? (int) (a + 4294967296L) : (int) a;
			return b;
		} catch (Exception ex) {
			return 0;
		}
	}
}
完整JAVA代码
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 ? tools.charCodeAt(c, 0) - 87 : tools.Number(c);
		long c1 = '+' == b.charAt(d + 1) ? a >> c0 : a << c0;
		a = '+' == b.charAt(d) ? a + c1 & 4294967295L : a ^ c1;
	}
	a = tools.Number(a);
	return a + "";
}

public static String tk(String a, String TKK) {
	String[] e = TKK.split("\\.");
	int d = 0;
	int h = 0;
	int[] g = new int[a.length() * 3];
	h = tools.Number(e[0]);
	for (int f = 0; f < a.length(); f++) {
		int c = tools.charCodeAt(a, f);
		if (128 > c) {
			g[d++] = c;
		} else {
			if (2048 > c) {
				g[d++] = c >> 6 | 192;
			} else {
				if (55296 == (c & 64512) && f + 1 < a.length() && 56320 == (tools.charCodeAt(a, f + 1) & 64512)) {
					c = 65536 + ((c & 1023) << 10) + tools.charCodeAt(a, ++f) & 1023;
					g[d++] = c >> 18 | 240;
					g[d++] = c >> 12 & 63 | 128;
				} else {
					g[d++] = c >> 12 | 224;
					g[d++] = c >> 6 & 63 | 128;
					g[d++] = c & 63 | 128;
				}
			}
		}
	}
	int gl = 0;
	for (int b : g) {
		if (b != 0) {
			gl++;
		}
	}
	int[] g0 = new int[gl];
	gl = 0;
	for (int c : g) {
		if (c != 0) {
			g0[gl] = c;
			gl++;
		}
	}
	long aa = h;
	for (d = 0; d < g0.length; d++) {
		aa += g0[d];
		aa = tools.Number(b(aa, "+-a^+6"));
	}
	aa = tools.Number(b(aa, "+-3^+b+-f"));
	long bb = aa ^ tools.Number(e[1]);
	aa = bb;
	aa = aa + bb;
	bb = aa - bb;
	aa = aa - bb;
	if (0 > aa) {
		aa = (aa & 2147483647) + 2147483648L;
	}
	aa %= (long) 1e6;
	return aa + "" + "." + (aa ^ h);
}

csdn下载地址(Google翻译JAVA版demo 可运行)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值