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


Groups > comp.lang.python > #27498

Re: How to convert base 10 to base 2?

Date 2012-08-20 16:52 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
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>
Newsgroups comp.lang.python
Message-ID <mailman.3560.1345474364.4697.python-list@python.org> (permalink)

Show all headers | View raw


gianpycea@gmail.com wrote:
> On Monday, August 20, 2012 9:50:53 AM UTC+2, (unknown) 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!
>>     
>
> Thank you all for the big help!
> @Mark Lawrence
> Yes, you're definetely right: i should have posted my OS and the version but being a very rough question on syntax i thought it didn't really matter.
> I've quite a good general programming experience. I know Java,Visual Basic.NET,Pascal and Mathematica.
>   
note that the builtin bin function is not available with python ver < 2.6

def Denary2Binary(n):
    '''convert denary integer n to binary string bStr'''
    bStr = ''
    if n < 0:  raise ValueError, "must be a positive integer"
    if n == 0: return '0'
    while n > 0:
        bStr = str(n % 2) + bStr
        n = n >> 1
    return bStr

JM

(not my function but I can't remember who I stole from)

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