Last active
December 14, 2020 18:49
-
-
Save TrueBrain/5dc9da276f86259df358a2e928fc2fdc to your computer and use it in GitHub Desktop.
Replace \x escape in C-strings to \u escape
This file contains 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 re | |
import sys | |
def replace_utf8(match): | |
char = match.group(0) | |
char = chr(int(char[2:4], 16)) + chr(int(char[6:8], 16)) | |
char = char.encode("raw_unicode_escape").decode() | |
char = char.encode("unicode_escape").decode() | |
char = char.replace("\\x", "\\u00") | |
return char | |
with open(sys.argv[1]) as fp: | |
for line in fp.readlines(): | |
line = re.sub(r'(\\x[0-9A-Z][0-9A-Z]\\x[0-9A-Z][0-9A-Z])', replace_utf8, line) | |
print(line[:-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment