converting decimal to binary
SFBayling
a at b.c
Wed Jun 4 09:41:24 EDT 2003
"Tim Peters" <tim_one at email.msn.com> wrote in
news:mailman.1053741576.26298.python-list at python.org:
> [Michael P. Soulier]
>>> I'm trying to find a simple way to convert a decimal number to a
>>> binary string representation. I could do this the hard way, but as
>>> it's a one-liner in perl, I'm hoping there's an easy way to do this
>>> in python.
> OTOH, there's curious asymmetry here:
>
>>>> int('00011000000', 2)
> 192
>>>>
>
> That is, "the other direction" is built in.
I was hoping there would be something to continue the existing patterns
like;
hex(value)
oct(value)
bin(value)
or perhaps
print "%d" % value #Decimal
print "%x" % value #Hex
print "%o" % value #Octal
print "%b" % value #Binary
I ended up finding the baseconvert module in the CookBook as well and
changing it to default to decimal->binary.
def baseconvert(number, fromdigits=BASE10, todigits=BASE2):
newsgroup-post-testingly yours,
sfb.
---
(First time back in newsgroups in a while, I can only hope this works
properly).
More information about the Python-list
mailing list