Ascii to binary conversion

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

    Ascii to binary conversion

    Hy folks,

    I googled, and searched, and can not bealive that I have not found a
    built in way to convert the easy and elegant python way a function to
    easily convert simple ascii data to binary and back.

    I've written some my own but they were pretty slow using binascii
    linbrary with hexifly and unhexifly functions conbined with a
    lookuptable of binary and hex values.

    Any idea how to easily write a function that recieves a character or
    string and returns a binary number like:
    ascii("1") is converted to bin("00110001")
  • John Machin

    #2
    Re: Ascii to binary conversion

    On Aug 9, 11:18 pm, azrael <jura.gro...@gm ail.comwrote:
    Hy folks,
    >
    I googled, and searched, and can not bealive that I have not found a
    built in way to convert the easy and elegant python way a function to
    easily convert simple ascii data to binary and back.
    >
    I've written some my own but they were pretty slow using binascii
    linbrary with hexifly and unhexifly functions conbined with a
    lookuptable of binary and hex values.
    >
    Any idea how to easily write a function that recieves a character or
    string and returns a binary number like:
    ascii("1") is converted to bin("00110001")
    Here's one way:
    >>def a2b(a):
    .... ai = ord(a)
    .... return ''.join('01'[(ai >x) & 1] for x in xrange(7, -1, -1))
    ....
    >>a2b('1')
    '00110001'
    >>a2b('2')
    '00110010'
    >>a2b(chr(0))
    '00000000'
    >>a2b(chr(255 ))
    '11111111'
    >>>
    BUT ... why are you doing this so much that the speed matters???

    Comment

    • azrael

      #3
      Re: Ascii to binary conversion

      looks nice. is there an oposite function of ord() so I could also
      bring a binary number also back to ascii.

      the speed matters if you plan to exchange about 10 M ascii chars and
      don't wont to wait a year for the results. :)



      On 9 kol, 15:39, John Machin <sjmac...@lexic on.netwrote:
      On Aug 9, 11:18 pm, azrael <jura.gro...@gm ail.comwrote:
      >
      Hy folks,
      >
      I googled, and searched, and can not bealive that I have not found a
      built in way to convert the easy and elegant python way a function to
      easily convert simple ascii data to binary and back.
      >
      I've written some my own but they were pretty slow using binascii
      linbrary with hexifly and unhexifly functions conbined with a
      lookuptable of binary and hex values.
      >
      Any idea how to easily write a function that recieves a character or
      string and returns a binary number like:
      ascii("1") is converted to bin("00110001")
      >
      Here's one way:
      >
      >def a2b(a):
      >
      ...    ai = ord(a)
      ...    return ''.join('01'[(ai >x) & 1] for x in xrange(7, -1, -1))
      ...
      >
      >a2b('1')
      '00110001'
      >a2b('2')
      '00110010'
      >a2b(chr(0))
      '00000000'
      >a2b(chr(255) )
      '11111111'
      >
      BUT ... why are you doing this so much that the speed matters???

      Comment

      • Larry Bates

        #4
        Re: Ascii to binary conversion

        azrael wrote:
        looks nice. is there an oposite function of ord() so I could also
        bring a binary number also back to ascii.
        >
        the speed matters if you plan to exchange about 10 M ascii chars and
        don't wont to wait a year for the results. :)
        >
        >
        >
        On 9 kol, 15:39, John Machin <sjmac...@lexic on.netwrote:
        >On Aug 9, 11:18 pm, azrael <jura.gro...@gm ail.comwrote:
        >>
        >>Hy folks,
        >>I googled, and searched, and can not bealive that I have not found a
        >>built in way to convert the easy and elegant python way a function to
        >>easily convert simple ascii data to binary and back.
        >>I've written some my own but they were pretty slow using binascii
        >>linbrary with hexifly and unhexifly functions conbined with a
        >>lookuptable of binary and hex values.
        >>Any idea how to easily write a function that recieves a character or
        >>string and returns a binary number like:
        >>ascii("1") is converted to bin("00110001")
        >Here's one way:
        >>
        >>>>def a2b(a):
        >... ai = ord(a)
        >... return ''.join('01'[(ai >x) & 1] for x in xrange(7, -1, -1))
        >...
        >>
        >>>>a2b('1')
        >'00110001'
        >>>>a2b('2')
        >'00110010'
        >>>>a2b(chr(0 ))
        >'00000000'
        >>>>a2b(chr(255 ))
        >'11111111'
        >>
        >BUT ... why are you doing this so much that the speed matters???
        >
        Opposite of ord() is chr(). These functions have been available in every
        language I've used for the last 30 years. I would suggest that you might want
        to spend a little time reading a good Python book and to work through the
        tutorial. It will be worth your investment of time.

        -Larry

        Comment

        • Mensanator

          #5
          Re: Ascii to binary conversion

          On Aug 9, 8:18�am, azrael <jura.gro...@gm ail.comwrote:
          Hy folks,
          >
          I googled, and searched, and can not bealive that I have not found a
          built in way to convert the easy and elegant python way a function to
          easily convert simple ascii data to binary and back.
          >
          I've written some my own but they were pretty slow using binascii
          linbrary with hexifly and unhexifly functions conbined with a
          lookuptable of binary and hex values.
          >
          Any idea how to easily write a function that recieves a character or
          string and returns a binary number like:
          ascii("1") is converted to bin("00110001")
          In Pthon 2.6 & 3.0 there actually IS a bin() function.

          IDLE 2.6b1
          >>bin(ord('1' ))
          '0b110001'

          But it has that worthless '0b' decorator, but that's
          easily dealt with.
          >>bin(ord('1' ))[2:]
          '110001'

          And you won't get leading 0s unless you ask for them.
          >>bin(ord('1' ))[2:].zfill(8)
          '00110001'

          If you have previous Python versions (and even if you
          have 2.6), you might consider getting the gmpy module.
          It will convert to any base from 2 to 36 and in addition,
          provides a nice set of binary manipulation functions
          such as popcount (number of 1-bits), scan1 (finds posistion
          of first 1-bit), Hamming Distance (number of differing
          bits between two numbers), etc.
          >>import gmpy
          >>gmpy.digits(o rd('1')) # base 10 by default
          '49'
          >>gmpy.digits(o rd('1'),2) # same in base 2 (note - no decorator)
          '110001'
          >>gmpy.digits(o rd('1'),2).zfil l(8) # add leading 0s
          '00110001'
          >>gmpy.popcount (ord('1')) # of 1-bits
          3
          >>gmpy.scan1(or d('1')) # the first 1 is at bit 0
          0
          >>gmpy.scan1(or d('1'),1) # the next 1 is at bit 4
          4
          >>gmpy.hamdist( ord('1'),ord('6 ')) # differ at 3 bit positions
          3

          And you probably won't find anything faster than gmpy.

          Comment

          • azrael

            #6
            Re: Ascii to binary conversion

            You see, I don't like reading some tutorials. I pick myself a problem
            and look for ways to slove it. I am using Python for about 2 years,
            but mostly for image processing. As you say, ord is oposite to chr. I
            learn by example.

            thnx guys, this looks great


            On 9 kol, 16:47, Larry Bates <larry.ba...@we bsafe.com`wrote :
            azrael wrote:
            looks nice. is there an oposite function of ord() so I could also
            bring a binary number also back to ascii.
            >
            the speed matters if you plan to exchange about 10 M ascii chars and
            don't wont to wait a year for the results. :)
            >
            On 9 kol, 15:39, John Machin <sjmac...@lexic on.netwrote:
            On Aug 9, 11:18 pm, azrael <jura.gro...@gm ail.comwrote:
            >
            >Hy folks,
            >I googled, and searched, and can not bealive that I have not found a
            >built in way to convert the easy and elegant python way a function to
            >easily convert simple ascii data to binary and back.
            >I've written some my own but they were pretty slow using binascii
            >linbrary with hexifly and unhexifly functions conbined with a
            >lookuptable of binary and hex values.
            >Any idea how to easily write a function that recieves a character or
            >string and returns a binary number like:
            >ascii("1") is converted to bin("00110001")
            Here's one way:
            >
            >>>def a2b(a):
            ...    ai = ord(a)
            ...    return ''.join('01'[(ai >x) & 1] for x in xrange(7, -1, -1))
            ...
            >
            >>>a2b('1')
            '00110001'
            >>>a2b('2')
            '00110010'
            >>>a2b(chr(0) )
            '00000000'
            >>>a2b(chr(255) )
            '11111111'
            >
            BUT ... why are you doing this so much that the speed matters???
            >
            Opposite of ord() is chr().  These functions have been available in every
            language I've used for the last 30 years.  I would suggest that you might want
            to spend a little time reading a good Python book and to work through the
            tutorial.  It will be worth your investment of time.
            >
            -Larry

            Comment

            • John Machin

              #7
              Re: Ascii to binary conversion

              On Aug 10, 12:37 am, azrael <jura.gro...@gm ail.comwrote:
              [top-posting corrected]
              >
              On 9 kol, 15:39, John Machin <sjmac...@lexic on.netwrote:
              >
              On Aug 9, 11:18 pm, azrael <jura.gro...@gm ail.comwrote:
              >
              Hy folks,
              >
              I googled, and searched, and can not bealive that I have not found a
              built in way to convert the easy and elegant python way a function to
              easily convert simple ascii data to binary and back.
              >
              I've written some my own but they were pretty slow using binascii
              linbrary with hexifly and unhexifly functions conbined with a
              lookuptable of binary and hex values.
              >
              Any idea how to easily write a function that recieves a character or
              string and returns a binary number like:
              ascii("1") is converted to bin("00110001")
              >why are you doing this so much that the speed matters???
              the speed matters if you plan to exchange about 10 M ascii chars and
              don't wont to wait a year for the results. :)
              WHY do you plan to change about 10 M ascii chars into about 90 M
              bytes? Who or what is going to consume the output?

              Comment

              • Terry Reedy

                #8
                Re: Ascii to binary conversion



                azrael wrote:
                Any idea how to easily write a function that recieves a character or
                string and returns a binary number like:
                ascii("1") is converted to bin("00110001")
                Other alternatives in 2.6 (I believe) and 3.0:
                >>0b001110101 01
                469
                >>b='11101010 1'
                >>eval('0b'+b )
                469
                >>'{0:b}'.forma t(469)
                '111010101'

                Comment

                Working...