<?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>Compiled Thoughts by John Ciacia &#187; binary</title>
	<atom:link href="http://www.johnciacia.com/tag/binary/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnciacia.com</link>
	<description>Science, Technology, and Beyond</description>
	<lastBuildDate>Fri, 06 Jan 2012 15:46:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Binary to Decimal Conversion</title>
		<link>http://www.johnciacia.com/2009/05/21/133/</link>
		<comments>http://www.johnciacia.com/2009/05/21/133/#comments</comments>
		<pubDate>Thu, 21 May 2009 05:08:00 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[decimal]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=133</guid>
		<description><![CDATA[Converts a binary number to its decimal equivalent. However unlike the bindec function, this will preserve the binary/radix point. Code [code] [/code] Usage [code]echo bin2dec ( "1011101.1000101", true ); [/code] Output [code]1 + 4 + 8 + 16 + 64 + 0.5 + 0.03125 + 0.0078125 = 93.5390625[/code] Usage [code]echo bin2dec ( "1011101.1000101"); [/code] Output [...]]]></description>
			<content:encoded><![CDATA[<p>Converts a binary number to its decimal equivalent. However unlike the <a href="http://us3.php.net/manual/en/function.bindec.php">bindec </a>function, this will preserve the <a href="http://en.wikipedia.org/wiki/Binary_point">binary/radix point</a>.</p>
<p><strong>Code</strong><br />
[code]<?php</p>
<p>/**<br />
 * Convert a binary number with our without<br />
 * a radix point to its decimal equivalent.<br />
 *<br />
 * @param $binary The binary number to convert.<br />
 * @param $output Show the calculations.<br />
 * @return The decimal conversion<br />
 */<br />
function bin2dec($binary, $output = false) {<br />
    $N = 0;<br />
    $o = "";<br />
    list ( $rhs, $lhs ) = explode ( ".", $binary );<br />
    $rhs = strrev ( $rhs );<br />
    for($i = 0; $i < strlen ( $rhs ); $i ++) {<br />
        $d = $rhs [$i] * pow ( 2, $i );<br />
        $N = $d + $N;<br />
        $o = ($d == 0) ? $o : $o . $d . " + ";<br />
    }</p>
<p>    for($i = 0; $i < strlen ( $lhs ); $i ++) {<br />
        $d = $lhs [$i] * pow ( 2, - ($i + 1) );<br />
        $N = $d + $N;<br />
        $o = ($d == 0) ? $o : $o . $d . " + ";<br />
    }</p>
<p>    return ($output) ? substr ( $o, 0, - 3 ) . " = " . $N : $N;<br />
}</p>
<p>?> [/code]</p>
<p><strong>Usage</strong><br />
[code]echo bin2dec ( "1011101.1000101", true );  [/code]<br />
<strong>Output</strong><br />
[code]1 + 4 + 8 + 16 + 64 + 0.5 + 0.03125 + 0.0078125 = 93.5390625[/code]<br />
<strong>Usage</strong><br />
[code]echo bin2dec ( "1011101.1000101");  [/code]<br />
<strong>Output</strong><br />
[code]93.5390625[/code]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2009/05/21/133/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.johnciacia.com @ 2012-02-05 10:46:22 -->
