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


Groups > comp.lang.python > #27545

Re: How to convert base 10 to base 2?

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject 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> <mailman.3573.1345485660.4697.python-list@python.org>
Date 2012-08-20 21:05 -0700
Message-ID <7xpq6kga92.fsf@ruckus.brouhaha.com> (permalink)
Organization Nightsong/Fort GNOX

Show all headers | View raw


Ian Kelly <ian.g.kelly@gmail.com> writes:
> Everybody should know the generic algorithm, though:
> from itertools import chain ...

For n>0, assuming you just want the converted digits and not a string.
String conversion and minus sign for n<0 left as exercise.  Note this
returns a generator that you can convert to a list with list(...).

    def convert(n, base):
        a,b = divmod(n,base)
        if a > 0:
           for e in convert(a,base):
                 yield e
        yield b

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