Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'wed,': 0.03; 'typing': 0.05; 'pm,': 0.10; 'wrote:': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pad': 0.16; 'str()': 0.16; 'header:In-Reply-To:1': 0.21; 'received:209.85.210.174': 0.23; 'received:mail-iy0-f174.google.com': 0.23; '\xa0if': 0.23; 'skip:( 30': 0.24; 'code': 0.24; 'message-id:@mail.gmail.com': 0.28; 'relatively': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'rather': 0.34; 'there': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'skip:z 10': 0.40; 'more': 0.60; 'saw': 0.64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=MN2kP9C8fHM0uucaMfETsWxJPo/nx/I+Ps3TkSvNCQ4=; b=iPBOpDaGku4PpE0rIwmd97KqerTh/j23hUb6oiNewqxdCaIBjULbgIqcOL22ulJkXi PoN/i+hCLpGSXX1czzdFFK0SN/gk7o9DNrdRar03nw2+PzOEQ+5O4Z+vLrqx7EBjRl5j zVgpD6FkMatBIaAGkFitlxdGvybiV5x1vb/W0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=VDCErmf3u5mKEI53OiVNVoNmiZEknxlsyu0q05sauMZrp7M3VFk4GLZ5bOB58YEe/u BHlpibr+MhRiCad797AuaB43FPiyw8ZqTs9FOy8gK/lvqThd18dVKK4AJMe6NDkwoPIU ux4Pv9hJtTo7QtNfe+yIWNRSfymm5XwNOjAV8= MIME-Version: 1.0 In-Reply-To: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> References: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> Date: Wed, 15 Jun 2011 22:48:35 +1000 Subject: Re: integer to binary 0-padded From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 17 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308142119 news.xs4all.nl 49180 [::ffff:82.94.164.166]:41876 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7692 On Wed, Jun 15, 2011 at 10:29 PM, Olivier LEMAIRE wrote: > =A0 =A0b =3D str(bin(number))[2:] > =A0 =A0if len(b) !=3Dsize: > =A0 =A0 =A0 =A0b =3D (size-len(b))*"0"+b You don't need the str() there as bin() already returns a number. Here's a relatively trivial simplification - although it does make the code more cryptic: b =3D (size*"0"+bin(number)[2:])[-size:] After typing this up, I saw Daniel's post, which is rather better. Go with that one for zero-filling; mine's more general though, you can pad with other tokens. ChrisA