9

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'

5 Answers 5

7

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! God I love stackoverflow!
On a side note, is there anyway to do this on python versions< 2.6?
I think that ''.join([r'\x%x'%ord(c) for c in mystring]) will probably work back to python2.4 at least ... (whenever list-comps were introduced) -- And in general, from a performance standpoint, it is always better to join a list comprehension compared to a generator expression in Cpython ... So there's no harm in using that other than being a somewhat ugly style.
Indeed it does sir! I really appreciate all the help, being new to python, this help is really valuable to me!
@mgilson I'm afraid neither the original answer nor the comment are correct. Your hex format specifier should be %02x or {0:02x}, otherwise characters with an ordinal value less than 16 will generate an invalid \x escape sequence, being only a single hex digit!
6

Based on Jon Clements's answer, try the codes on python3.7. I have the error like this:

>>> s = '1234'    
>>> hexlify(s)    
Traceback (most recent call last):    
  File "<pyshell#13>", line 1, in <module>    
    hexlify(s)    
TypeError: a bytes-like object is required, not 'str'

Solved by the following codes:

>>> str = '1234'.encode()    
>>> hexlify(str).decode()   
'31323334'

Comments

5

Something like:

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

1 Comment

Nice solution. I think that this is the best answer
3

Try:

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

1 Comment

This seems to be nearly working, this seemingly works on versions < 2.6 which is nice. Instead of '\x48\x65\x6c\x6c' you get '0x48 0x65 0x6c 0x6c' Nearly done fixing it, but if someone has a fix, would love to see it
3

Starting from Python 3.5 you can use hex method to convert string to hex values (resulting in a string):

str = '1234'
str.encode().hex()
# '31323334'

Knowing that it's possible to resolve it in yet another way:

str = '1234'
hexed = str.encode().hex()
hex_values = [hexed[i:i+2] for i in range(0, len(hexed), 2)] # every 2 chars
delim = r'\x'

res = delim + delim.join(hex_values)
print(res)
# '\x31\x32\x33\x34'

Note: You can omit encode() method if you define your string as bytes: str = b'1234'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.