Skip to content

Instantly share code, notes, and snippets.

@TrueBrain
Last active December 14, 2020 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TrueBrain/5dc9da276f86259df358a2e928fc2fdc to your computer and use it in GitHub Desktop.
Save TrueBrain/5dc9da276f86259df358a2e928fc2fdc to your computer and use it in GitHub Desktop.
Replace \x escape in C-strings to \u escape
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