Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'syntax': 0.03; 'binary': 0.05; 'default.': 0.07; 'work!': 0.07; 'subject:How': 0.09; 'stored': 0.10; 'aug': 0.13; '12:50': 0.16; 'integer.': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'integer': 0.17; 'specify': 0.17; '>>>': 0.18; 'python?': 0.20; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'am,': 0.27; 'first.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'argue': 0.29; 'base,': 0.29; "i'm": 0.29; 'goes': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'doing': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; '20,': 0.65 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:x-gm-message-state; bh=eEpQWc5dkYjwffYGXDqmebQ0lzeG7z+0i+kEvGCEfs0=; b=Mi5EvcSShzfM6NvU8TT8IWExxajrxv+UaOVaUqI2NtgXGYQXtrAiaNeExKvvQnKQPZ TXn19M1ntrb4Q33gs63MaV5nYp2UdL0CDVVuleWwV6bEbAyZ2Kd9c2xeYx/e/v3yP+xi zzCaTcqSgunG1b5/HcKAvTyfCY74CvIr7E1Ypbr2t+68dxX/IqJJ+vS368Xxb0H9Ej0Q qxU3haE3U+VJbpqWraF+l1aISnse4YGZ4/bfDk6OtydpXoz8pAPCv+GgBF1eS/pwuGrC Fn55Hp7boX90v5Fa66Fv3TcQ/Pb0qmZb65xAfv0zA6OHGzBbsFkQ4VQDSfMrW/TPTANK EH3Q== MIME-Version: 1.0 In-Reply-To: <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> References: <1cedbf80-117b-48aa-a9f2-754293203408@googlegroups.com> Date: Mon, 20 Aug 2012 01:04:22 -0700 Subject: Re: How to convert base 10 to base 2? From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-Gm-Message-State: ALoCoQkX5ie1R7a8ou996qjccSaIObrL/GSiVFrbHE8tq1rc8MBcbzxI4dWotaP0U5yNvBWvQPrM X-Junkmail-Whitelist: YES (by domain whitelist at mpv2.tis.cwru.edu) 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345449933 news.xs4all.nl 6942 [2001:888:2000:d::a6]:60342 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27473 On Mon, Aug 20, 2012 at 12:50 AM, 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'