<?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>Defcrypt &#187; Binario</title>
	<atom:link href="http://defcrypt.ixde.net/index.php/tag/binario/feed/" rel="self" type="application/rss+xml" />
	<link>http://defcrypt.ixde.net</link>
	<description>Blog sobre programación y seguridad informática</description>
	<lastBuildDate>Sun, 09 Aug 2009 21:20:56 +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>Hexadecimal y Binario en Python</title>
		<link>http://defcrypt.ixde.net/index.php/2008/12/hexadecimal-y-binario-en-python/</link>
		<comments>http://defcrypt.ixde.net/index.php/2008/12/hexadecimal-y-binario-en-python/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 18:37:29 +0000</pubDate>
		<dc:creator>dGil</dc:creator>
				<category><![CDATA[Programación]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutoriales]]></category>
		<category><![CDATA[Binario]]></category>
		<category><![CDATA[Hexadecimal]]></category>
		<category><![CDATA[Octal]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://defcrypt.ixde.net/?p=51</guid>
		<description><![CDATA[Python, como la mayoría de lenguajes, permite trabajar con hexadecimal (base 16), decimal (base 10), octal (base 8) y a partir de la versión 2.6 también se puede usar binario (base 2). Aunque parezca raro que no se implementara base binaria hasta la versión 2.6, normalmente el uso de binario se puede substiuir por hexadecimal. [...]]]></description>
			<content:encoded><![CDATA[<p>Python, como la mayoría de lenguajes, permite trabajar con hexadecimal (base 16), decimal (base 10), octal (base 8) y a partir de la versión 2.6 también se puede usar binario (base 2). Aunque parezca raro que no se implementara base binaria hasta la versión 2.6, normalmente el uso de binario se puede substiuir por hexadecimal. Aún así, se agradece que podamos escojer nosotros.</p>
<p>Para insertar un número en hexadecimal se utiliza el 0x delante del numero en hexadecimal:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">numhex = 0x2f
<span style="color: #ff7700;font-weight:bold;">print</span> numhex <span style="color: #808080; font-style: italic;"># 47</span></pre></div></div>

<p>En python, siempre que se haga un print y no se indique lo contrario, se imprimen los números en base 10.</p>
<p>Ejemplo de inserción de binario y octal (para identificar un número como binario tiene que empezar con 0b y en el caso de octal por 0):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">numbin = 0b1101
<span style="color: #ff7700;font-weight:bold;">print</span> numbin <span style="color: #808080; font-style: italic;"># 13</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">numoct = 011
<span style="color: #ff7700;font-weight:bold;">print</span> numoct <span style="color: #808080; font-style: italic;"># 9</span></pre></div></div>

<p>Para poder imprimir los números en cada base tendremos que hacer lo siguiente:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Hexadecimal</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%x&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">10</span>   <span style="color: #808080; font-style: italic;"># 0xA</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">hex</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># 0xA</span>
<span style="color: #808080; font-style: italic;"># Octal</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%o&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">10</span>  <span style="color: #808080; font-style: italic;"># 012</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">oct</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># 012</span>
<span style="color: #808080; font-style: italic;"># Binario</span>
<span style="color: #ff7700;font-weight:bold;">print</span> bin<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># 0b1010</span></pre></div></div>

<p>Para finalizar, comentar que al usar hex(), oct() o bin() retornan un string, no un int.</p>
]]></content:encoded>
			<wfw:commentRss>http://defcrypt.ixde.net/index.php/2008/12/hexadecimal-y-binario-en-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
