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?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=5729fed13=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'syntax': 0.03; 'binary': 0.05; 'builtin': 0.07; 'work!': 0.07; 'subject:How': 0.09; 'python': 0.09; 'lawrence': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; "'0'": 0.16; 'right:': 0.16; 'rough': 0.16; 'valueerror,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'integer': 0.17; '(not': 0.20; 'python?': 0.20; 'posted': 0.22; 'cc:2**0': 0.23; 'programming': 0.23; 'monday,': 0.23; "i've": 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'question': 0.27; '2.6': 0.27; "doesn't": 0.28; 'argue': 0.29; "i'm": 0.29; 'function': 0.30; 'hi,': 0.33; 'version': 0.34; "can't": 0.34; 'subject:?': 0.35; 'really': 0.36; 'but': 0.36; "didn't": 0.36; 'should': 0.36; 'thank': 0.36; 'being': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'received:194': 0.61; 'email addr:gmail.com': 0.63; 'experience.': 0.65; '20,': 0.65; 'matter.': 0.65; 'august': 0.66
X-IronPort-AV E=Sophos;i="4.77,798,1336341600"; d="scan'208";a="664547"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Mon, 20 Aug 2012 16:52:42 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To gianpycea@gmail.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>
In-Reply-To <28b3f583-fad1-46da-a6b5-933f966eb401@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3560.1345474364.4697.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345474364 news.xs4all.nl 6919 [2001:888:2000:d::a6]:51259
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27498

Show key headers only | 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