Skip to content

Instantly share code, notes, and snippets.

@rschiang
Last active December 24, 2015 04:29
Show Gist options
  • Save rschiang/6744150 to your computer and use it in GitHub Desktop.
Save rschiang/6744150 to your computer and use it in GitHub Desktop.
嘗試還原遺失ASCII高位元的Big5字串。
import codecs, itertools
decode = codecs.getdecoder('big5')
f = open('input.txt', 'r')
l = 8 # 8-byte decode block
L = 2 ** l # Use bit-flag to do permutations
d = f.read()
for pointer in range(0, len(d), l):
s = d[pointer:pointer+l]
occured = []
for iter in range(L + 1):
str = ''
for i in range(l):
if ((2 ** i) & iter) == 0:
str += s[i]
else:
str += chr(ord(s[i]) | 0x80)
S = decode(str, 'ignore')[0]
if S in occured: continue
print S
occured.append(S)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment