Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7693
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: integer to binary 0-padded |
| Followup-To | comp.lang.python |
| Date | Wed, 15 Jun 2011 14:54:41 +0200 |
| Organization | None |
| Lines | 22 |
| Message-ID | <itaa1d$a57$1@solani.org> (permalink) |
| References | <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | solani.org 1308142446 10407 eJwNycEBwCAIA8CVNJBQxqkU9h/B3vdo2qpwUc7hHOUCIrLJZSbmzOMb/yOKhZjW6x+7ANm59/0QIw== (15 Jun 2011 12:54:06 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Wed, 15 Jun 2011 12:54:06 +0000 (UTC) |
| X-User-ID | eJwFwYEBwCAIA7CXUGmBc4bS/09YgsPFG07QIaibLvOsPWc9vaZufWlpQrAzcyZY4MryjV3IwVyFPpO9+wNS8xWR |
| Cancel-Lock | sha1:19MSkBxzNZ/tcK7cEnNBX3m+uUo= |
| X-NNTP-Posting-Host | eJwNyskNwEAIA8CWlsOQlMMC7r+ERJrnwEKi0wPhINhxAGE9Zs+dNxujQq6KVa5xLwfIXW//8RQpU1VyqJ2l+Y+u8jumOPcDA2IbtA== |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:7693 |
Followups directed to: comp.lang.python
Show key headers only | View raw
Olivier LEMAIRE wrote:
> 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 I wrote the following:
> b = str(bin(number))[2:]
The result of bin() is already a string, no need to apply str().
> if len(b) !=size:
> b = (size-len(b))*"0"+b
b.zfill(size) can do that.
> Though, what do you think about it ?
Here's another way (requires Python 2.7):
>>> "{:0{}b}".format(42, 10)
'0000101010'
Back to comp.lang.python | Previous | Next — Previous 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