Binary, Hex, and Decimal string conversions

Robert Dailey rcdailey at gmail.com
Mon Aug 13 01:20:49 CEST 2007


Well, I decided to implement my own way of doing this. I've attached the
source. You're all welcome :)

On 8/12/07, Michael Bentley <michael at jedimindworks.com> wrote:
>
> Hi Robert,
> On Aug 11, 2007, at 3:59 PM, Robert Dailey wrote:
>
> Hi, I was wondering if there is a built in module that supports conversion
> in any direction between Binary, Hex, and Decimal strings? Thanks.
>
>
> Shouldn't be too hard to build one.  Here's a little incantation to
> convert from base 10 to another base:
>
> import string
>
> def to_base(number, base):
> 'converts base 10 integer to another base'
>
> number = int(number)
> base = int(base)
> if base < 2 or base > 36:
> raise ValueError, "Base must be between 2 and 36"
> if not number:
> return 0
>  symbols = string.digits + string.lowercase[:26]
> answer = []
> while number:
> number, remainder = divmod(number, base)
> answer.append(symbols[remainder])
> return ''.join(reversed(answer))
>
> How 'bout you hack a from_base function and email it back to me? (hint:
> type 'help(int)' in the python interpreter).
>
> Peace,
> Michael
>
> ---
> Let the wookie win.
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070812/c5ee1221/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: baseconv.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20070812/c5ee1221/attachment.ksh>


More information about the Python-list mailing list