1

Is there a simple way (or simplest?) to convert between '\x1a\x2b\x3c\x4d' and '1a2b3c4d' in python?

(Here '\x4d' means a byte whose ascii code is 4d, namely the character 'M', but '4d' stand for two characters. And the others are similar meanings.)

Daniel
  • 1,783
  • 2
  • 15
  • 25
  • possible duplicate of [Convert hex string to int in Python](http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python) –  Jan 10 '15 at 17:00
  • @importV, they're actually not the same question. – Daniel Jan 10 '15 at 17:07

1 Answers1

7

You can convert bytes to their hex representation with the binascii.hexlify() function:

>>> import binascii
>>> binascii.hexlify('\x1a\x2b\x3c\x4d')
'1a2b3c4d'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343