Skip to content

Instantly share code, notes, and snippets.

@goyusia
Created December 17, 2015 09:28
Show Gist options
  • Select an option

  • Save goyusia/3e5e10d7142aad905869 to your computer and use it in GitHub Desktop.

Select an option

Save goyusia/3e5e10d7142aad905869 to your computer and use it in GitHub Desktop.
calculate tk for translate-shell
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
'''
$ ./calc_tk.py
text = 안녕하세요 테스트입니다
tk = 513628.127973
'''
text = u"안녕하세요 테스트입니다"
array = [int(x) for x in list(text.encode("utf-8"))]
#print(array)
import ctypes
def RL(a, b):
#print("initial a: {}".format(a))
for c in range(0, len(b) - 2, 3):
t = ord('a')
Tb = ord("+")
d = b[c + 2]
#pdb.set_trace()
if ord(d) >= t:
d = ord(d) - 87
else:
d = int(d)
#print("first a: {}, d: {}".format(a, d))
if ord(b[c + 1]) == Tb:
#d = a >> d
tmp = ctypes.c_uint32(a)
tmp.value >>= d
d = tmp.value
else:
tmp = ctypes.c_int32(a)
tmp.value <<= d
d = tmp.value
#d = a << d
#print("before a: {}, d: {}".format(a, d))
if ord(b[c]) == Tb:
#a = a + d & 4294967295
xa, xd, xc = ctypes.c_int32(a), ctypes.c_int32(d), ctypes.c_int32(4294967295)
a = xa.value + xd.value & xc.value
else:
a = a ^ d
#print("after a: {}".format(a))
#print("final a: {}\n\n".format(a))
return a
#SL = 402872
SL = 402873
b = SL
d = array
a = b
Vb = "+-a^+6"
Ub = "+-3^+b+-f"
for e in range(0, len(d)):
a += d[e]
#print("step {} : a={}".format(e, a))
a = RL(a, Vb)
a = RL(a, Ub)
if 0 > a:
a = (a & 2147483647) + 2147483648
a = int(a % 1E6)
tk="{}.{}".format(a, a ^ b)
print("text = {}".format(text))
print("tk = {}".format(tk))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment