Convert Python str/unicode object to binary/hex blob

Viewed 13974

Is there an easy way to get some str/unicode object represented as a big binary number (or an hex one)?

I've been reading some answers to related questions but none of them works for my scenario.

I tried using the struct module from the STL but it didn't work as expected. Chars, like in binary files are displayed as, well chars.

Am I trying something impossible?

Example:

def strbin(inp):
    # sorcery!
    return out

>> print strbin("hello")
# Any of these is cool (outputs are random keystrokes)
0b1001010101010000111001110001...
0xad9f...
5 Answers
Related