How to remove '\x' from a hex string in Python?

14,888

Solution 1

Indeed, you don't have backslashes in your string. So, that's why you can't remove them.

If you try to play with each hex character from this string (using ord() and len() functions - you'll see their real values. Besides, the length of your string is just 4, not 16.

You can play with several solutions to achieve your result: 'hex' encode:

'\xff\x1f\x00\xe8'.encode('hex')
'ff1f00e8'

Or use repr() function:

repr('\xff\x1f\x00\xe8').translate(None,r'\\x')

Solution 2

One way to do what you want is:

>>> s = '\xff\x1f\x00\xe8'
>>> ''.join('%02x' % ord(c) for c in s)
'ff1f00e8'

The reason why translate is not working is that what you are seeing is not the string itself, but its representation. In other words, \x is not contained in the string:

>>> '\\x' in '\xff\x1f\x00\xe8'
False

\xff, \x1f, \x00 and \xe8 are the hexadecimal representation of for characters (in fact, len(s) == 4, not 24).

Solution 3

Use the encode method:

>>> s = '\xff\x1f\x00\xe8'
>>> print s.encode("hex")
'ff1f00e8'
Share:
14,888

Related videos on Youtube

Ebrahim Ghasemi
Author by

Ebrahim Ghasemi

Passionate Java Card programmer with +6 years of experience in different security related topics, including cryptography, web application and network penetration testing and also reverse engineering. Having strong background in network traffic analysis, deep packet inspection,networking protocols and high-performance system programming.

Updated on June 18, 2022

Comments

  • Ebrahim Ghasemi
    Ebrahim Ghasemi almost 2 years

    I'm reading a wav audio file in Python using wave module. The readframe() function in this library returns frames as hex string. I want to remove \x of this string, but translate() function doesn't work as I want:

    >>> input = wave.open(r"G:\Workspace\wav\1.wav",'r')
    >>> input.readframes (1)
    '\xff\x1f\x00\xe8'
    >>> '\xff\x1f\x00\xe8'.translate(None,'\\x')
    '\xff\x1f\x00\xe8'
    >>> '\xff\x1f\x00\xe8'.translate(None,'\x')
    ValueError: invalid \x escape
    >>> '\xff\x1f\x00\xe8'.translate(None,r'\x')
    '\xff\x1f\x00\xe8'
    >>> 
    

    Any way I want divide the result values by 2 and then add \x again and generate a new wav file containing these new values. Does any one have any better idea?

    What's wrong?

  • Ebrahim Ghasemi
    Ebrahim Ghasemi over 8 years
    Does it have any reverse way to? I mean how can I convert 'ff1f00e8' to '\xff\x1f\x00\xe8' again? any Decode function?
  • Dmitry
    Dmitry over 8 years
    Reverse: 'ff1f00e8'.decode('hex') :) What about length - was playing with backslashes in the initial string.