Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7688 > unrolled thread

integer to binary 0-padded

Started byOlivier LEMAIRE <m.olivier.lemaire@gmail.com>
First post2011-06-15 05:29 -0700
Last post2011-06-15 14:54 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#7688 — integer to binary 0-padded

FromOlivier LEMAIRE <m.olivier.lemaire@gmail.com>
Date2011-06-15 05:29 -0700
Subjectinteger to binary 0-padded
Message-ID<5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com>
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 = 00000010
I wrote the following:
<code>
#!/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 positive integer"
    if type(size)!=int:
	raise ValueError, "should be an integer"
    if number > (2**size)-1:
	raise ValueError, "number is too large"
    # convert int to bin
    b = str(bin(number))[2:]

    if len(b) !=size:
	b = (size-len(b))*"0"+b

    return b


if __name__ == "__main__":
    import sys
    print int2binPadded(int(sys.argv[1]),int(sys.argv[2]))
</code>

This satisfies my needs !

Though, what do you think about it ?

Thank you for you remarks,

Olivier

[toc] | [next] | [standalone]


#7690

FromDaniel Rentz <daniel.rentz@gmx.de>
Date2011-06-15 14:33 +0200
Message-ID<ita8qe$iqn$1@news-cedar.fernuni-hagen.de>
In reply to#7688
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 = 00000010

bin(2)[2:].zfill(8)


Regards
Daniel

[toc] | [prev] | [next] | [standalone]


#7696

FromTim Chase <python.list@tim.thechases.com>
Date2011-06-15 08:13 -0500
Message-ID<mailman.2.1308143602.1164.python-list@python.org>
In reply to#7690
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 = 00000010
>
> bin(2)[2:].zfill(8)

Just to have in the thread (though the OP seems to be using 2.6 
or later), the bin() function appears to have been added in 2.6 
which is something I wish had been back-ported to 2.4 and 2.5 as 
I've been sufficiently stuck coding against older versions of 
Python to have (re)written a bin() function multiple times.  Ah well.

-tkc


[toc] | [prev] | [next] | [standalone]


#7692

FromChris Angelico <rosuav@gmail.com>
Date2011-06-15 22:48 +1000
Message-ID<mailman.1.1308142118.1164.python-list@python.org>
In reply to#7688
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

[toc] | [prev] | [next] | [standalone]


#7693

FromPeter Otten <__peter__@web.de>
Date2011-06-15 14:54 +0200
Message-ID<itaa1d$a57$1@solani.org>
In reply to#7688
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'

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web