<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Trux &#187; python</title>
	<atom:link href="http://trux.info/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://trux.info</link>
	<description>Partage de mes astuces et découvertes</description>
	<lastBuildDate>Tue, 09 Jun 2009 19:47:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python : convertir un entier en binaire</title>
		<link>http://trux.info/2009/01/python-convertir-un-entier-en-binaire/</link>
		<comments>http://trux.info/2009/01/python-convertir-un-entier-en-binaire/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 20:50:36 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Informatique]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://trux.info/?p=53</guid>
		<description><![CDATA[Depuis python 2.6, le langage python peut gérer les nombres entier dans leur représentation binaire.

$ python2.6
>>> 0b101111
47
>>> int('1101', 2)
13
>>> int('0b1101', 2)
13
>>> bin(13)
'0b1101'

Cependant, avec les versions précédentes, seule la conversion de binaire vers décimal est possible facilement.

$ python2.5
>>> int('1101', 2)
13
>>> bin(13)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'bin' is not [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://docs.python.org/whatsnew/2.6.html#pep-3127-integer-literal-support-and-syntax">Depuis python 2.6</a>, le langage python peut gérer les nombres entier dans leur représentation binaire.</p>
<pre>
$ python2.6
>>> 0b101111
47
>>> int('1101', 2)
13
>>> int('0b1101', 2)
13
>>> bin(13)
'0b1101'
</pre>
<p>Cependant, avec les versions précédentes, seule la conversion de binaire vers décimal est possible facilement.</p>
<pre>
$ python2.5
>>> int('1101', 2)
13
>>> bin(13)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'bin' is not defined
</pre>
<p>Voici donc une petite fonction <code>bin</code> :</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> bin<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Convert an integer to its binary string representation&quot;&quot;&quot;</span>
    res = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">&gt;</span> 0:
        res.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>0, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        n = n // <span style="color: #ff4500;">2</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>res<span style="color: black;">&#41;</span></pre></div></div>

<p>Et ainsi :</p>
<pre>
$ python2.5
>>> def bin(n):
...     """Convertit un nombre en binaire"""
...     res = []
...     while n > 0:
...         res.insert(0, str(int(n % 2)))
...         n = n // 2
...     return "".join(res)
...
>>> bin(13)
'1101'
>>> int(bin(13), 2)
13
</pre>
]]></content:encoded>
			<wfw:commentRss>http://trux.info/2009/01/python-convertir-un-entier-en-binaire/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
