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 = 000

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:]

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 D

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,

Re: integer to binary...

2006-06-04 Thread Bruno Desthuilliers
Grant Edwards a écrit : > On 2006-06-02, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > >>Grant Edwards a écrit : >> >>>On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >>> >>>>does anyone know a module or something to con

Re: integer to binary...

2006-06-03 Thread Grant Edwards
On 2006-06-03, Tim Chase <[EMAIL PROTECTED]> wrote: >> The fact that they impliment the xor operator is pretty much >> proof that integers are stored in binary format -- xor is only >> defined for binary numbers. > > Um...let's not use bad logic/proofs for evidencing this... > > >>> hasattr(set(),

Re: integer to binary...

2006-06-03 Thread Tim Chase
> The fact that they impliment the xor operator is pretty much > proof that integers are stored in binary format -- xor is only > defined for binary numbers. Um...let's not use bad logic/proofs for evidencing this... >>> hasattr(set(), "__xor__") True :) -tkc -- http://mail.python.org/mail

Re: integer to binary...

2006-06-03 Thread Grant Edwards
On 2006-06-02, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Grant Edwards a écrit : >> On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >> >> >>>does anyone know a module or something to convert numbers like integer >>>to bin

Re: integer to binary...

2006-06-02 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > Grant Edwards wrote: > >>On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >> >> >>>does anyone know a module or something to convert numbers like integer >>>to binary format ? >> >>They _are_ in

Re: integer to binary...

2006-06-02 Thread Bruno Desthuilliers
Grant Edwards a écrit : > On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > >>does anyone know a module or something to convert numbers like integer >>to binary format ? > > > They _are_ in binary format. Not really. >>> (7)

Re: integer to binary...

2006-06-02 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > does anyone know a module or something to convert numbers like integer > to binary format ? > > for example I want to convert number 7 to 0111 so I can make some > bitwise operations... You don't need to convert anything. The bitwise ops are &am

Re: integer to binary...

2006-06-02 Thread Sion Arrowsmith
Tim Chase <[EMAIL PROTECTED]> wrote: >bitCount = len([c for c in "01001010101" if c=="1"]) bitCount = "01001010101".count("1") -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ ___ | "Frankly I have no feelings towards penguins one way or the other" \X/ |-- Arthur C. Clark

Re: integer to binary...

2006-06-02 Thread John Salerno
[EMAIL PROTECTED] wrote: > Use the gmpy module. Yes, it's good. :) -- http://mail.python.org/mailman/listinfo/python-list

Re: integer to binary...

2006-06-02 Thread Claudio Grondi
[EMAIL PROTECTED] wrote: > [EMAIL PROTECTED] wrote: > >>does anyone know a module or something to convert numbers like integer >>to binary format ? >> >>for example I want to convert number 7 to 0111 so I can make some >>bitwise operations... >

Re: integer to binary...

2006-06-01 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote: > does anyone know a module or something to convert numbers like integer > to binary format ? > > for example I want to convert number 7 to 0111 so I can make some > bitwise operations... > > Thanks Use the gmpy module. >>> import gmpy

Re: integer to binary...

2006-06-01 Thread Grant Edwards
On 2006-06-01, Tim Chase <[EMAIL PROTECTED]> wrote: for example I want to convert number 7 to 0111 so I can make some bitwise operations... >>> Just do it: >>> >> 7 & 3 >>> 3 >> 7 | 8 >>> 15 >> I know I can do that but I need to operate in every bit separeted. > > > I suppose ther

Re: integer to binary...

2006-06-01 Thread Grant Edwards
On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >>>> does anyone know a module or something to convert numbers like integer >>>> to binary format ? >>> >>> They _are_ in binary format. >>> >>>> for example I want t

Re: integer to binary...

2006-06-01 Thread Grant Edwards
On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >>> does anyone know a module or something to convert numbers like >>> integer to binary format ? >> >> They _are_ in binary format. >> >> > for example I want to convert number 7

Re: integer to binary...

2006-06-01 Thread nicolasg
[EMAIL PROTECTED] wrote: > Grant Edwards wrote: > > On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > does anyone know a module or something to convert numbers like integer > > > to binary format ? > > > > They _are_ in bin

Re: integer to binary...

2006-06-01 Thread Anton Vredegoor
[EMAIL PROTECTED] wrote: > does anyone know a module or something to convert numbers like integer > to binary format ? > > for example I want to convert number 7 to 0111 so I can make some > bitwise operations... >>> def bits(i,n): return tuple((0,1)[i>>

Re: integer to binary...

2006-06-01 Thread Tim Chase
>>> for example I want to convert number 7 to 0111 so I can make some >>> bitwise operations... >> Just do it: >> > 7 & 3 >> 3 > 7 | 8 >> 15 > I know I can do that but I need to operate in every bit separeted. I suppose there might be other operations for which having them as strings cou

Re: integer to binary...

2006-06-01 Thread nicolasg
Grant Edwards wrote: > On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > does anyone know a module or something to convert numbers like integer > > to binary format ? > > They _are_ in binary format. > > > for example I want to conv

Re: integer to binary...

2006-06-01 Thread Alexis Roda
En/na [EMAIL PROTECTED] ha escrit: > does anyone know a module or something to convert numbers like integer > to binary format ? http://www.google.es/search?q=python+integer+to+binary http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300 > for example I want to convert number

Re: integer to binary...

2006-06-01 Thread Grant Edwards
On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > does anyone know a module or something to convert numbers like integer > to binary format ? They _are_ in binary format. > for example I want to convert number 7 to 0111 so I can make some > bitwise operations...

Re: integer to binary...

2006-06-01 Thread Schüle Daniel
[EMAIL PROTECTED] schrieb: > does anyone know a module or something to convert numbers like integer > to binary format ? unfortunately there is no builtin function for this >>> int("111",2) 7 >>> str(7) '7' >>> str(7,2) Traceback (most

integer to binary...

2006-06-01 Thread nicolasg
does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... Thanks -- http://mail.python.org/mailman/listinfo/python-list