I have a list of values from a parsed PE file that include /x00 null bytes at the end of each section. I want to be able to remove the /x00 bytes from the string without removing all "x"s from the file. I have tried doing .replace and re.sub, but not which much success.
Using Python 2.6.6
Example.
import re
List = [['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
while count < len(List):
test = re.sub('\\\\x00', '', str(list[count])
print test
count += 1
>>>test (removes x, but I want to keep it) #changed from tet to test
>>>data
>>>rsrc
I want to get the following output
text data rsrc
Any ideas on the best way of going about this?
The b" notation is used to specify a bytes string in Python. Compared to the regular strings, which have ASCII characters, the bytes string is an array of byte variables where each hexadecimal element has a value between 0 and 255.
\x00 is thus a byte with all its bits at 0. (As Ryne pointed out, a null character translates to this.) Other examples: \xff is 11111111, \x7f is 01111111, \x80 is 10000000, \x2c is 00101010, etc.
>>> L = [['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
>>> [[x[0]] for x in L]
[['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
>>> [[x[0].replace('\x00', '')] for x in L]
[['.text'], ['.data'], ['.rsrc']]
Or to modify the list in place instead of creating a new one:
for x in L:
x[0] = x[0].replace('\x00', '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With