integer to binary 0-padded

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jun 18 05:00:29 EDT 2011


On Fri, 17 Jun 2011 23:14:09 -0700, jmfauth wrote:

>>>> '{:+#0{}b}'.format(255, 1 + 2 + 16)
> +0b0000000011111111
>>>> '{:+#0{}b}'.format(-255, 1 + 2 + 16)
> -0b0000000011111111
>>>>
>>>> eval('{:+#0{}b}'.format(255, 1 + 2 + 16))
> 255
>>>> eval('{:+#0{}b}'.format(-255, 1 + 2 + 16))
> -255


Is this a question? Or did you just decide to share some code?

Please leave enough context so that readers can understand what you are 
talking about.



-- 
Steven



More information about the Python-list mailing list