简体   繁体   中英

Remove hex character from string in Python

I'm trying to remove one character from a string o hex values but I can't find a solution to make this work. If I print remove2 I get the string "\x41", but when I print buffer I get ABCD". The thing is I don't understand why when I print remove2 I get the string hex format and when I print buffer I get the ASCII format. I think it is in the root of the problem. How could I fix this using Python 2?

>>> buffer = "\x41\x42\x43\x44"
>>> remove = raw_input("Enter hex value to remove: ")
Enter hex value to remove: 42
>>> remove2 = "\\x" + remove
>>> print buffer
ABCD
>>> print remove2
\x42
>>> buffer2 = buffer.replace(remove2, '')
>>> print buffer2
ABCD

I wish buffer2 = "\x41\x43\x44".

Here's the problem:

remove2 = "\\x" + remove

You can't programmatically build escape sequences like that. Instead, do this:

remove2 = chr(int(remove, 16))

Alternatively, you'd have to make buffer contain the literal backslashes instead of the escaped characters:

buffer = "\\x41\\x42\\x43\\x44"

The problem being is that if you print out remove without the print, you'll see

>>> remove2
'\\x42'

that the \ is staying there and not making it hexadecimal. For that you need to do:

remove.decode('hex')

so the code being:

>>> buffer = "\x41\x42\x43\x44"
>>> remove = raw_input("Enter hex value to remove: ")
Enter hex value to remove: 42
>>> remove2=remove.decode('hex')
>>> buffer.replace(remove2, '')
'ACD'

Does that help/answers your question?

You will need to escape the \ in your buffer string o/w it will be treated as hex value. So,

>>> buffer="\\x41\\x42\\x43"`<br>
>>> remove = "42"`<br>
>>> remove = "\\x" + remove` <br>
>>> buffer = buffer.replace(remove, '')` <br>
>>> print buffer #prints \\\x41\\\x43

You can use filter() and construct a filtered bytes object using the user input of "42" and original bytes (just a string in Python2).

>>> inp = "42"
>>> filter(lambda x: x != chr(int(inp, 16)), 'ABCD')
'ACD'

Python 3

>>> inp = "42"
>>> bytes(filter(lambda x: x != int(inp, 16), b'ABCD'))
b'ACD'

Anyway, simpler to use replace() , this is just an alternative way to filter out specific values from a bytes object. It illustrates the basic idea other answers point out. The user input needs to be correctly converted to the value you intend to remove.

When the interp renders the output, the backslashes aren't represented in bytes or str objects for characters/values that correspond to utf-8 or ascii printable characters. If there isn't a corresponding printable character, then an escaped version of the value will be presented in output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM