Ascii to binary conversion
larry.bates at websafe.com`
Sat Aug 9 16:47:53 CEST 2008
> 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... at lexicon.net> wrote:
>> On Aug 9, 11:18 pm, azrael <jura.gro... at gmail.com> wrote:
>>> 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))
>> 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.
More information about the Python-list