Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

int 2 binary: What do you think?

34 views
Skip to first unread message

Martin Bless

unread,
Apr 23, 2001, 9:31:01 AM4/23/01
to
I needed to convert integers to binary representation and couldn't
find a builtin function. So here's what I came up with. The idea is to
return a string as short as possible and have positive numbers always
start with a "0".

I wonder if there's a better way?

And most probably there are lots of modules around offering a
solution, aren't they?

Martin Bless


b_nibbles = [
'0000', '0001', '0010', '0011',
'0100', '0101', '0110', '0111',
'1000', '1001', '1010', '1011',
'1100', '1101', '1110', '1111']

# Another way to create b_nibbles:
# b_nibbles = [a+b+c+d for a in ['0','1'] for b in ['0','1'] \
# for c in ['0','1'] for d in ['0','1']]

def i2bstr(arg):
"""Return integer as binary string."""
import sys
isPositive = (arg >= 0)
maskHighNibble = sys.maxint >> 3
r = ''
while 1:
r = b_nibbles[arg & 0xF] + r
arg = (arg >> 4) & maskHighNibble
if not arg:
if isPositive and r[0] == '1':
return '0000' + r
else:
return r

def test_i2bstr():
"""Demonstration of i2bstr()."""
import sys
integers = [0,1,15,16,255,256,32767,32768,
sys.maxint,-sys.maxint-1,-256,-32768, -1]
for i in integers:
print "%12d %s" % (i, i2bstr(i))

## Output:
## 0 0000
## 1 0001
## 15 00001111
## 16 00010000
## 255 000011111111
## 256 000100000000
## 32767 0111111111111111
## 32768 00001000000000000000
## 2147483647 01111111111111111111111111111111
## -2147483648 10000000000000000000000000000000
## -256 11111111111111111111111100000000
## -32768 11111111111111111000000000000000
## -1 11111111111111111111111111111111


Andrew Gaul

unread,
May 10, 2001, 9:08:28 AM5/10/01
to
In article <3ae42b7a...@news.muenster.de>, Martin Bless wrote:
>I needed to convert integers to binary representation and couldn't
>find a builtin function. So here's what I came up with. The idea is to
>return a string as short as possible and have positive numbers always
>start with a "0".
>
>I wonder if there's a better way?

Here's a shorter implementation; it may be more efficient:

def bin(i):
l = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',


'1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']

s = ''.join(map(lambda x, l=l: l[int(x, 16)], hex(i)[2:]))
if s[0] == '1' and i > 0:
s = '0000' + s
return s

It'd be nice if an equivalent built-in function was added in a future
version of Python.

--
| a | n | d | r | e | w | @ | g | a | u | l | . | o | r | g |
White trash. Loser. Geek.

Alex Martelli

unread,
May 10, 2001, 9:36:21 AM5/10/01
to
"Andrew Gaul" <ga...@spam.utexas.edu> wrote in message
news:slrn9fl4m...@meatring.cs.utexas.edu...
...

> It'd be nice if an equivalent built-in function was added in a future
> version of Python.

If such a builtin was added, I think it should not focus solely
on binary representation, but allow any base from 2 up to and
including 36, just like int() and long() do with their optional
2nd argument, but of course "in reverse".

In fact, perhaps adding a 2nd optional argument to str() might
be a better architecture than adding a new builtin!


Alex

Martin Bless

unread,
May 13, 2001, 3:56:27 PM5/13/01
to
[Andrew Gaul]

>Here's a shorter implementation; it may be more efficient:

I bet it is, very nice. I see, hex() is very helpful.
And having map() running along a string bilding a list of strings -
I still have to learn what can be accomplished by combining simple
techniques in Python ...

>def bin(i):
> l = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
> '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']
> s = ''.join(map(lambda x, l=l: l[int(x, 16)], hex(i)[2:]))
> if s[0] == '1' and i > 0:
> s = '0000' + s
> return s

bin() is a good name - considering the existing hex(). I'll keep this
version.

>It'd be nice if an equivalent built-in function was added in a future
>version of Python.

Yes.

Martin

0 new messages