Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27517

Re: How to convert base 10 to base 2?

References <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> <28b3f583-fad1-46da-a6b5-933f966eb401@googlegroups.com> <50324F3A.7040001@sequans.com> <uvr4385905sjupcdb6mkee2jf4gq7tjlch@invalid.netcom.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-08-20 12:00 -0600
Subject Re: How to convert base 10 to base 2?
Newsgroups comp.lang.python
Message-ID <mailman.3573.1345485660.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Aug 20, 2012 at 11:29 AM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
>         I think I typically have done this by going through a hex
> representation.
>
> H2B_Lookup = {  "0" : "0000",   "1" : "0001",
>                                         "2" : "0010",   "3" : "0011",
>                                         "4" : "0100",   "5" : "0101",
>                                         "6" : "0110",   "7" : "0111",
>                                         "8" : "1000",   "9" : "1001",
>                                         "A" : "1010",   "B" : "1011",
>                                         "C" : "1100",   "D" : "1101",
>                                         "D" : "1110",   "F" : "1111"    }
>
> def I2B(i):
>     sgn = " "
>     if i < 0:
>         sgn = "-"
>         i = -i
>     h = ("%X" % i)
>     return sgn + "".join([H2B_Lookup[c] for c in h])
>
>>>> from i2b import I2B
>>>> I2B(10)
> ' 1010'
>>>> I2B(1238)
> ' 010011100110'
>>>> I2B(-6)
> '-0110'
>>>>

I would throw a .lstrip('0') in there to get rid of the ugly leading
zeroes (and also add a special case for i == 0).

Everybody should know the generic algorithm, though:

from itertools import chain

def convert(n, base):
    digits = [chr(x) for x in chain(range(ord('0'), ord('9')+1),
                                    range(ord('A'), ord('Z')+1))]
    if not 2 <= base <= len(digits):
        raise ValueError("invalid base")
    output = []
    sign = ""
    if n < 0:
        n = -n
        sign = "-"
    while n > 0:
        n, r = divmod(n, base)
        output.append(digits[r])
    return sign + ''.join(reversed(output)) or '0'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to convert base 10 to base 2? gianpycea@gmail.com - 2012-08-20 00:50 -0700
  Re: How to convert base 10 to base 2? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-08-20 01:04 -0700
    Re: How to convert base 10 to base 2? Miki Tebeka <miki.tebeka@gmail.com> - 2012-08-21 09:23 -0700
    Re: How to convert base 10 to base 2? Miki Tebeka <miki.tebeka@gmail.com> - 2012-08-21 09:23 -0700
  Re: How to convert base 10 to base 2? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-20 09:10 +0100
  Re: How to convert base 10 to base 2? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-20 09:26 +0100
  Re: How to convert base 10 to base 2? gianpycea@gmail.com - 2012-08-20 01:57 -0700
    Re: How to convert base 10 to base 2? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-20 16:52 +0200
    Re: How to convert base 10 to base 2? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-20 13:29 -0400
    Re: How to convert base 10 to base 2? Joel Goldstick <joel.goldstick@gmail.com> - 2012-08-20 13:57 -0400
    Re: How to convert base 10 to base 2? Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:00 -0600
      Re: How to convert base 10 to base 2? Paul Rubin <no.email@nospam.invalid> - 2012-08-20 21:05 -0700
    Re: How to convert base 10 to base 2? Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-20 12:10 -0600

csiph-web