Skip to content

Instantly share code, notes, and snippets.

@mkpoli
Created July 2, 2020 09:26
Show Gist options
  • Select an option

  • Save mkpoli/8effa9275182556abf953ebcd4639267 to your computer and use it in GitHub Desktop.

Select an option

Save mkpoli/8effa9275182556abf953ebcd4639267 to your computer and use it in GitHub Desktop.
East-Asian Mojibake Encoding Detection (Manual) / 亂碼轉換 / 乱码转换 / 文字化け変換
ENCODINGS = {
# Japanese
'shift_jis': "Shift-JIS",
'iso2022_jp': "ISO 2022 JP",
# Chinese
'big5': "Big5",
'gbk': "GBK",
'gb2312': "GB 2312",
'gb18030': "GB 18030",
# Korean
"euc_kr": "EUC-KR",
"iso2022_kr": "ISO 2022 KR",
"johab": "Johab",
"cp949": "UHC",
# Unicode Transformation Format
'utf_8': "UTF-8",
}
def try_decode(text: bytes, without: str):
for encoding, encoding_name in ENCODINGS.items():
if encoding == without:
continue
try:
decoded = text.decode(encoding, 'replace')
yield (encoding, encoding_name, decoded)
except UnicodeDecodeError as e:
continue
def pprint_hex(byte_str: bytes):
return ' '.join(["%02X" % x for x in byte_str])
def try_encode(text: str, without: str):
for encoding, encoding_name in ENCODINGS.items():
if encoding == without:
continue
try:
encoded = text.encode(encoding)
yield (encoding, encoding_name, encoded)
except UnicodeEncodeError as e:
continue
def main():
C = input()
encoding_name_maxlen = len(max(ENCODINGS.values(), key=len))
for encoding, encoding_name, encoded in try_encode(C, ""):
print(f"Success! Encoded into { encoding_name } = ‘{pprint_hex(encoded)}’")
for _, encoding_name_decode, decoded in try_decode(encoded, encoding):
print(f"-- Decoded from {encoding_name_decode.ljust(encoding_name_maxlen)}: {decoded}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment