469,643 Members | 1,486 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

binascii.a2b_binary

Why is there no binascii.a2b_binary(bitstr) which returns the binary data
represented by the bit string? Like:
binascii.a2b_binary('0011001100110101') '35'

perl has pack("B*", "0011001100110101");

What is the python way to do this?

Other fun with strings:
'3333'.decode('hex') '33' '3333'.encode('hex')

'33333333'

I easily found the doc for str.decode(), but it appears to only mention
encodings for languages. It took me a while to connect it to hex_codec.
Maybe a more direct link could be added.

Thanks,
-EdS
Apr 3 '06 #1
2 1743
Ed Swarthout wrote:
Why is there no binascii.a2b_binary(bitstr) which returns the binary data
represented by the bit string? Like:
binascii.a2b_binary('0011001100110101')

'35'
perl has pack("B*", "0011001100110101");


What, you mean like:
int('0011001100110101', 2)
Which you could show as:
hex(int('0011001100110101', 2))

I guess because Python is not so wonderful as Perl. Apparently Python
stupidly forgot to follow Perl's great naming conventions.

--Scott David Daniels
sc***********@acm.org
Apr 3 '06 #2
Ed Swarthout wrote:
Why is there no binascii.a2b_binary(bitstr) which returns the binary data
represented by the bit string? Like:
binascii.a2b_binary('0011001100110101')

'35'

perl has pack("B*", "0011001100110101");

What is the python way to do this?


to post a question on comp.lang.python and have the code written by
somebody else :)

def a2b_binary(s):
return ''.join(chr(int(s[pos:pos+8],2)) for pos in
range(0,len(s),8))

Apr 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Mercuro | last post: by
9 posts views Thread by py | last post: by
12 posts views Thread by Larry Bates | last post: by
1 post views Thread by Jeremy | last post: by
10 posts views Thread by Chason Hayes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.