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
>>> '{:+#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
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
You're right, I use Python 2.6.6
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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()
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
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
--
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
10 matches
Mail list logo