Last active
December 24, 2015 04:29
-
-
Save rschiang/6744150 to your computer and use it in GitHub Desktop.
嘗試還原遺失ASCII高位元的Big5字串。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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