integer to binary 0-padded

O

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
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
 
D

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

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


Regards
Daniel
 
C

Chris Angelico

   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
 
P

Peter Otten

Olivier said:
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):
'0000101010'
 
T

Tim Chase

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top