I wrote a program that uses builtin function bin(), but this function is new in Python version 2.6 and I would like to run this application also in Python versions 2.4 and 2.5.
Is there some backport of bin() for 2.4?
You can try this version (credit goes to the original author):
def bin(x):
"""
bin(number) -> string
Stringifies an int or long in base 2.
"""
if x < 0:
return '-' + bin(-x)
out = []
if x == 0:
out.append('0')
while x > 0:
out.append('01'[x & 1])
x >>= 1
pass
try:
return '0b' + ''.join(reversed(out))
except NameError, ne2:
out.reverse()
return '0b' + ''.join(out)