1

If I type "32510ba9babebbbefd001547a810e67149caee".decode('hex') into the shell the output looks like 2Q\x0b\xa9\xba\xbe\xbb\xbe\xfd\x00\x15G\xa8\x10\xe6qI\xca\xee' which is what I want.

However if I run this from a .py script nothing gets printed. If I change the code to

print "32510ba9babebbbefd001547a810e67149caee".decode('hex')

I get all kinds of symbols displayed like ♬, for example

k29
  • 641
  • 6
  • 26

2 Answers2

3

This is similar to what the python shell does:

print repr("32510ba9babebbbefd001547a810e67149caee".decode('hex'))
gus27
  • 2,616
  • 1
  • 21
  • 25
  • @k29: this answer and naltipar's are subtly different - `repr` includes the surrounding quotes, `.encode('string-escape')` does not. – mhawke Apr 26 '15 at 11:49
  • True; both answers essentially do what I require: display the string as escaped hex. – k29 Apr 26 '15 at 11:55
2

Is this what you want?

print "32510ba9babebbbefd001547a810e67149caee".decode('hex').encode('string-escape')
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37