integer to binary 0-padded

Tim Chase python.list at tim.thechases.com
Wed Jun 15 09:13:04 EDT 2011


On 06/15/2011 07:33 AM, Daniel Rentz wrote:
> Am 15.06.2011 14:29, schrieb Olivier LEMAIRE:
>> Hi there, I've been looking for 2 days for a way to convert integer
>> to binary number 0-padded, nothing... I need to get numbers converted
>> with a defined number of bits. For example on 8 bits 2 = 00000010
>
> bin(2)[2:].zfill(8)

Just to have in the thread (though the OP seems to be using 2.6 
or later), the bin() function appears to have been added in 2.6 
which is something I wish had been back-ported to 2.4 and 2.5 as 
I've been sufficiently stuck coding against older versions of 
Python to have (re)written a bin() function multiple times.  Ah well.

-tkc






More information about the Python-list mailing list