10

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...
3
  • Are you trying to get an integer or a string of the hex/binary representation? In your example you have integers given as binary and hex literals. Commented Jul 18, 2011 at 2:20
  • @Peter, in most of the use scenarios I'm thinking of I would say an integer. This is just for some silly compression algorithm I thought some days ago, just for fun. Commented Jul 18, 2011 at 2:53
  • related: Convert binary to ASCII and vice versa Commented Nov 18, 2015 at 13:31

5 Answers 5

20

You could try bitarray:

>>> import bitarray
>>> b = bitarray.bitarray()
>>> b.fromstring('a')
>>> b
bitarray('01100001')
>>> b.to01()
'01100001'
>>> b.fromstring('pples')
>>> b.tostring()
'apples'
>>> b.to01()
'011000010111000001110000011011000110010101110011'
Sign up to request clarification or add additional context in comments.

4 Comments

I can't give you and upvote senderle (still 4 reputation left for that) but your answer is what I was looking for. Checking this as accepted answer :) Thank you so much.
I'll also mention bitstring, which I think is a tad bit more versatile, but it's pure Python, so it's slower.
There's no STL library that could this without some sorcery right? Just curious. Edit: Oh and you got the upboat.
@ratnushock, well, once you have an int, you could call bin on it. But that doesn't pad the byte, so it doesn't concatenate well. (hex doesn't pad either; hex(5) returns '0x5'.) JBernardo's answer gets the padding right and is as good as anything I can think of from the standard libraries.
7

Quite simple and don't require modules from pypi:

def strbin(s):
    return ''.join(format(ord(i),'0>8b') for i in s)

You'll need Python 2.6+ to use that.

2 Comments

Well I'm pretty sure that @senderle 's answer is gonna be faster just because it's a C extension but hey STL solutions are also great. Thanks for your answer!
Probably you don't need it to be that fast. Otherwise why would you write in Python at all? Quoting the Zen: Simple is better than complex
1
def strhex(str):
    h=""
    for x in str:
        h=h+(hex(ord(x)))[2:]
    return "0x"+h

2 Comments

This returns the ASCII number not the binary repr but it works in a similar way so.. Thanks for your answer!
Don't the above answers also return ASCII? If you need an int, you could add int(h,16) at the end.
1

A slice from a larger pretty print function I wrote that prints the ascii code in hex. Just a more Pythonic version of the previous answer's function. Also, it works properly for characters with single digit ascii codes.

def strhex(string, start = '0x'):
    return start + ''.join(('{:x}'.format(ord(char))).zfill(2) for char in string)

Comments

1

This is basically Ravi's answer but with a tiny bugfix, so all credit to him, but the consensus among reviewers was that this was too big of a change to just do an edit and should be a separate answer instead... No idea why.

def strhex(str):
    h=""
    for x in str:
        h=h+("0" + (hex(ord(x)))[2:])[-2:]
    return "0x"+h

The difference is that in line 4, you have to check if the character is less than 0x10 and in that case prepend a zero, otherwise e.g. 0x1101 becomes 0x111.

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.