How to convert a number to binary?

Nick Craig-Wood nick at craig-wood.com
Fri May 18 05:30:05 EDT 2007


Lyosha <lyoshaM at gmail.com> wrote:
>  On May 17, 11:04 pm, Stargaming <stargam... at gmail.com> wrote:
>  [...]
> > >>>Is there an *easy* way to convert a number to binary?
>  [...]
> >
> > Wrote this a few moons ago::
> >
> >    dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else ''
> 
>  This is awesome.  Exactly what I was looking for.  Works for other
>  bases too.

Just don't pass it a negative number ;-)

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list