Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7696

Re: integer to binary 0-padded

Date 2011-06-15 08:13 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: integer to binary 0-padded
References <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> <ita8qe$iqn$1@news-cedar.fernuni-hagen.de>
Newsgroups comp.lang.python
Message-ID <mailman.2.1308143602.1164.python-list@python.org> (permalink)

Show all headers | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

integer to binary 0-padded Olivier LEMAIRE <m.olivier.lemaire@gmail.com> - 2011-06-15 05:29 -0700
  Re: integer to binary 0-padded Daniel Rentz <daniel.rentz@gmx.de> - 2011-06-15 14:33 +0200
    Re: integer to binary 0-padded Tim Chase <python.list@tim.thechases.com> - 2011-06-15 08:13 -0500
  Re: integer to binary 0-padded Chris Angelico <rosuav@gmail.com> - 2011-06-15 22:48 +1000
  Re: integer to binary 0-padded Peter Otten <__peter__@web.de> - 2011-06-15 14:54 +0200

csiph-web