简体   繁体   中英

Converting all chars in a string to ascii hex in python

Just looking for python code that can turn all chars from a normal string(all english alphbetic letters) to ascii hex in python. I'm not sure if I'm asking this in the wrong way because i've been searching for this but can't seem to find this.

I must just be passing over the answer, but i would love some help.

Just to clarify, from 'Hell' to '\\x48\\x65\\x6c\\x6c'

I suppose ''.join(r'\\x{02:x}'.format(ord(c)) for c in mystring) would do the trick...

>>> mystring = "Hello World"
>>> print ''.join(r'\x{02:x}'.format(ord(c)) for c in mystring)
\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64

Something like:

>>> s = '123456'
>>> from binascii import hexlify
>>> hexlify(s)
'313233343536'

尝试:

" ".join([hex(ord(x)) for x in myString])

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