Python: binary/hex string conversion?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python: binary/hex string conversion?



I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex.



The string is as below:


<81>^Q<81>"^Q^@^[)^G ^Q^A^S^A^V^@<83>^Cd<80><99>}^@N^@^@^A^@^@^@^@^@^@^@j



How do I go about converting this string in Python so that the output in hex format is similar to this below?


24208040901811001B12050809081223431235113245422F0A23000000000000000000001F





can you elaborate on the intended translation? It appears to me that the example string and the hex output are not the same thing... is <81> a single, not-printable hexadecimally encoded character or is it a textual representation of this? I'm confused by the string holding binary charaters (what do you mean by that) and that hou want to convert it to binary, then to hex...
– Adriaan
Aug 6 '09 at 10:08





Do this: print(repr(your_string))) and copy/paste the result into your question. Tell us what version of Python and what platform.
– John Machin
Aug 6 '09 at 14:16


print(repr(your_string)))




3 Answers
3



You can use ord and hex like this :


>>> s = 'some string'
>>> hex_chars = map(hex,map(ord,s))
>>> print hex_chars
['0x73', '0x6f', '0x6d', '0x65', '0x20', '0x73', '0x74', '0x72', '0x69', '0x6e', '0x67']
>>> hex_string = "".join(c[2:4] for c in hex_chars)
>>> print hex_string
736f6d6520737472696e67
>>>



Or use the builtin encoding :


>>> s = 'some string'
>>> print s.encode('hex_codec')
736f6d6520737472696e67
>>>





Your version with hex and ord does not work reliably. Use "%2.2x".__mod__ instead of hex and you can also avoid the c[2:4]. As a result it would look like: "".join(map("%2.2x".__mod__, map(ord, s))). The encoding version is of course better. :-)
– Helmut Grohne
Feb 21 '11 at 13:19


hex


ord


c[2:4]


"".join(map("%2.2x".__mod__, map(ord, s)))





Great, answer!!
– Japanish
Sep 24 '15 at 13:21


>>> import binascii

>>> s = '2F'

>>> hex_str = binascii.b2a_hex(s)

>>> hex_str

>>> '3246'



OR


>>>import binascii

>>> hex_str = binascii.hexlify(s)

>>> hex_str
>>> '3246'
>>>



Faster solution see:


from timeit import Timer

import os
import binascii

def testSpeed(statement, setup = 'pass'):
print '%s' % statement
print '%s' % Timer(statement, setup).timeit()

setup = """
import os

value = os.urandom(32)
"""

# winner
statement = """
import binascii

binascii.hexlify(value)
"""

testSpeed(statement, setup)

# loser
statement = """
import binascii

value.encode('hex_codec')
"""

testSpeed(statement, setup)



Results:


import binascii

binascii.hexlify(value)

2.18547999816

value.encode('hex_codec')

2.91231595077






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS