Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"c"': 0.07; 'though:': 0.07; 'ugly': 0.07; 'subject:How': 0.09; '"a"': 0.09; 'received :mail-lpp01m010-f46.google.com': 0.09; 'throw': 0.09; 'def': 0.10; 'aug': 0.13; '"-"': 0.16; '"0"': 0.16; '"2"': 0.16; '"b"': 0.16; '"d"': 0.16; '"f"': 0.16; "'0'": 0.16; '0).': 0.16; 'hex': 0.16; 'itertools': 0.16; 'zeroes': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'received:209.85.215.46': 0.30; '(and': 0.32; "skip:' 20": 0.32; 'rid': 0.33; 'to:addr:python-list': 0.33; 'typically': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'generic': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'add': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'leading': 0.61; '20,': 0.65; 'everybody': 0.69; 'special': 0.73; 'algorithm,': 0.84; 'to:name:python': 0.84; 'dennis': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=d78Iszf6PDhfmi58h7/JLsEuIjepUIrZ3pg6dEItJKc=; b=vQWBcaGXipeZz7GNedqGgBgCf5FYEbhluH9qXuHwvJ+PEqbHu+elowvtMsH/G0QuH2 B3+3zPS9jEX9Yfx4NHDWx5o6DC3RsrjNESGjpwwunkm+ut4TkLvhPaZbMCSsut66ulxO weamZFgjD46TnYnbVyab6vn8MoRrfkFn++HoRq4fdFS9M7G6mb+8pR4NlAacYlGA3nww v2cRc8pcFr4/w76f2q4zU/usZ+hqc8C/emle+86gw6OnXazCxR/r92zWhTVXectsdP3J b6d096ksuwsBLGvfarL0gKWZjYUpS2zUucgIy/qlzKWek7Ea7s8sU+uYEO+cO5eIUI+6 xFSA== MIME-Version: 1.0 In-Reply-To: References: <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> <28b3f583-fad1-46da-a6b5-933f966eb401@googlegroups.com> <50324F3A.7040001@sequans.com> From: Ian Kelly Date: Mon, 20 Aug 2012 12:00:21 -0600 Subject: Re: How to convert base 10 to base 2? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345485660 news.xs4all.nl 6950 [2001:888:2000:d::a6]:39317 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27517 On Mon, Aug 20, 2012 at 11:29 AM, Dennis Lee Bieber 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'