[Python-3000] New built-in function: bin()

Mike Traynar mike_traynar at credence.com
Fri Apr 21 11:20:22 CEST 2006


I've always wondered why there isn't a bin() built-in in Python. 
Built-in functions already exist for converting ints to hexadecimal and 
octal strings and vice versa i.e.

    s = hex() and int(s, 16)
    s = oct() and int(s, 8)

but no bin() function for binary strings

    s = ??? and int(s, 2)?

Working with hardware I occasionally need to display an integer in 
binary format, or manipulate the bits of integer. A simple example would 
be reversing the order of the bits. Converting an int to binary string, 
reversing the string, then converting back to an int is a simple way to 
do this. Right now I use the recipe in 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300 and am 
happy to continue to do so, but it has always struck me as odd that 
there isn't a bin() built-in.

Mike

PS. I'll let someone else argue the case for the tri(), quad(), 
penta()... built-in functions.

  


More information about the Python-3000 mailing list