Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27473
| References | <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> |
|---|---|
| Date | 2012-08-20 01:04 -0700 |
| Subject | Re: How to convert base 10 to base 2? |
| From | Benjamin Kaplan <benjamin.kaplan@case.edu> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3544.1345449933.4697.python-list@python.org> (permalink) |
On Mon, Aug 20, 2012 at 12:50 AM, <gianpycea@gmail.com> wrote:
> Hi,
> as you can argue from the subject, i'm really,really new to python.
> What is the best way to achieve that with python? Because the syntax int('30',2) doesn't seem to work!
That syntax goes the other way- from a string representing a number in
the given base into an integer (which doesn't have a base, although
it's stored in base 2).
You get the binary by doing bin(x), where x is an integer.
>>> bin(12)
'0b1100'
If you want to go from a string in base-10 to a string in base-2, you
have to do the conversion to integer first.
>>> bin(int('5',10))
'0b101'
Although you don't need to specify the 10 because that's the default.
>>> bin(int('5'))
'0b101'
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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