bin2chr("01110011") # = 15 function ?

Tim Chase python.list at tim.thechases.com
Mon Feb 25 15:29:25 EST 2008


> I'm searching for a simple
> bin2chr("01110011") function that can convert all my 8bit data to a
> chr so I can use something like this:

> print ord("s") # = 115
> print bin(ord("s")) # = 01110011
> 
> test= open('output.ext,'wb')
> test.write(bin2chr("01110011"))
> test.write(bin2chr("01111011"))
> test.write(bin2chr("01110111"))
> test.close()

I think this simple one-liner should do it for you:

   >>> bin2char = lambda s: chr(int(s,2))
   >>> bin2char('01110011')
   's'

which exploits int()'s ability to take a base (2, in this case).

-tkc






More information about the Python-list mailing list