Re: integer to binary 0-padded

2011-06-18 Thread Steven D'Aprano
On Fri, 17 Jun 2011 23:14:09 -0700, jmfauth wrote: '{:+#0{}b}'.format(255, 1 + 2 + 16) > +0b '{:+#0{}b}'.format(-255, 1 + 2 + 16) > -0b eval('{:+#0{}b}'.format(255, 1 + 2 + 16)) > 255 eval('{:+#0{}b}'.format(-255, 1 + 2 + 16)) > -255 Is th

Re: integer to binary 0-padded

2011-06-17 Thread jmfauth
>>> '{:+#0{}b}'.format(255, 1 + 2 + 16) +0b >>> '{:+#0{}b}'.format(-255, 1 + 2 + 16) -0b >>> >>> eval('{:+#0{}b}'.format(255, 1 + 2 + 16)) 255 >>> eval('{:+#0{}b}'.format(-255, 1 + 2 + 16)) -255 >>> jmf -- http://mail.python.org/mailman/listinfo/python-list

Re: integer to binary 0-padded

2011-06-17 Thread John S
On Jun 15, 9:33 am, Olivier LEMAIRE wrote: > You're right, I use Python 2.6.6 This works great in 2.6.5 and later (and probably earlier). You just have to number your placeholders. The first set of braces formats i (your value), the second set specifies the field with (i.e., 8): >>> for i in xra

Re: integer to binary 0-padded

2011-06-15 Thread Olivier LEMAIRE
You're right, I use Python 2.6.6 -- http://mail.python.org/mailman/listinfo/python-list

Re: integer to binary 0-padded

2011-06-15 Thread Tim Chase
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 = 0010

Re: integer to binary 0-padded

2011-06-15 Thread Olivier LEMAIRE
Thank you to all of you !! so finally, I can simply write : #!/usr/bin/env python def int2binPadded(number, size): """The purpose of this function is to convert integer number to binary number 0-padded.""" if type(number)!=int or number < 0: raise ValueError, "should be a

Re: integer to binary 0-padded

2011-06-15 Thread Peter Otten
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 = 0010 I wrote the following: > b = str(bin(number))[2:] The result of bin()

Re: integer to binary 0-padded

2011-06-15 Thread Chris Angelico
On Wed, Jun 15, 2011 at 10:29 PM, Olivier LEMAIRE 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

Re: integer to binary 0-padded

2011-06-15 Thread Daniel Rentz
Hi, 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 = 0010 bin(2)[2:].zfill(8) Regards Daniel --

integer to binary 0-padded

2011-06-15 Thread 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 = 0010 I wrote the following: #!/usr/bin/env python def int2binPadded(number, size): """The