Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7692
| References | <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> |
|---|---|
| Date | 2011-06-15 22:48 +1000 |
| Subject | Re: integer to binary 0-padded |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1.1308142118.1164.python-list@python.org> (permalink) |
On Wed, Jun 15, 2011 at 10:29 PM, Olivier LEMAIRE <m.olivier.lemaire@gmail.com> wrote: > b = str(bin(number))[2:] > if len(b) !=size: > b = (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 = (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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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