Reputation: 467
I have a file that contains hexadecimal numbers that I want to convert to strings:
'\x73\x63\x6f\x72\x65\x73': '\x4c\x6f\x72\x65\x6d\x20\x69\x70\x73\x75\x6d',
'Status', ['\x75\x70\x64\x61\x74\x65']
But when using re.sub to replace each occurrence of a hexadecimal escaped number by its ascii representation, it doesn't seem to find the hexadecimal number in the first place.
I've tried using raw strings, but it didn't change anything. I still can't replace them.
import re, binascii
with open('hex.txt', 'r') as f:
file = f.read()
hexList = re.findall(r"'([\\x\w+]*)'", file)
for item in hexList:
file = re.sub(r"('{}')".format(item), str(binascii.unhexlify(item.replace('\\x', ''))), file)
#file = re.sub("('"+item+"')".format(item), str(binascii.unhexlify(item.replace('\\x', ''))), file)
print(file)```
Upvotes: 0
Views: 939
Reputation: 847
You can use the following code fragment
import re, binascii
with open('hex.txt', 'r') as f:
file = f.read()
hexList = re.findall(r'((?:\\x[0-9a-f][0-9a-f])+)', file)
for item in hexList:
file = re.sub(r"('{}')".format(item.replace('\\', '\\\\')), str(binascii.unhexlify(item.replace('\\x', ''))), file)
print(file)
The regex used by you for finding the hex-strings is wrong because it is even finding the Status
as the hex-string.
Upvotes: 2