binascii.a2b_binary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed Swarthout

    #1

    binascii.a2b_binary

    Why is there no binascii.a2b_bi nary(bitstr) which returns the binary data
    represented by the bit string? Like:
    [color=blue][color=green][color=darkred]
    >>> binascii.a2b_bi nary('001100110 0110101')[/color][/color][/color]
    '35'

    perl has pack("B*", "00110011001101 01");

    What is the python way to do this?

    Other fun with strings:
    [color=blue][color=green][color=darkred]
    >>> '3333'.decode(' hex')[/color][/color][/color]
    '33'[color=blue][color=green][color=darkred]
    >>> '3333'.encode(' hex')[/color][/color][/color]
    '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
  • Scott David Daniels

    #2
    Re: binascii.a2b_bi nary

    Ed Swarthout wrote:[color=blue]
    > Why is there no binascii.a2b_bi nary(bitstr) which returns the binary data
    > represented by the bit string? Like:
    >[color=green][color=darkred]
    >>>> binascii.a2b_bi nary('001100110 0110101')[/color][/color]
    > '35'
    > perl has pack("B*", "00110011001101 01");[/color]

    What, you mean like:
    int('0011001100 110101', 2)
    Which you could show as:
    hex(int('001100 1100110101', 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
    scott.daniels@a cm.org

    Comment

    • Serge Orlov

      #3
      Re: binascii.a2b_bi nary

      Ed Swarthout wrote:[color=blue]
      > Why is there no binascii.a2b_bi nary(bitstr) which returns the binary data
      > represented by the bit string? Like:
      >[color=green][color=darkred]
      > >>> binascii.a2b_bi nary('001100110 0110101')[/color][/color]
      > '35'
      >
      > perl has pack("B*", "00110011001101 01");
      >
      > What is the python way to do this?[/color]

      to post a question on comp.lang.pytho n 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))

      Comment

      Working...