[Tutor] Binary 2 text & text 2 binary

Poor Yorick tutor.python.org at pooryorick.com
Sat Sep 24 14:15:10 CEST 2005


Pujo Aji wrote:
> If your symbol are specific it is better to use dictionary.
> then if the user give an input you can take character by character and 
> translate into your binary.
> This is the code textTobinary:
>     mydic = {'A' : "01000001", 'B' : "01000010", 'C' : "01000011"}
>     strinput = 'ABBC'
>     result = [mydic[x] for x in strinput_process]
>     print result
>  

You might also want to look at "Number to String in Arbirtrary Base" recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/365468

It uses a more innovative approach.

--
Poor Yorick



More information about the Tutor mailing list