Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 2144

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Binary to decimal</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.base-convert.html">base_convert</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.ceil.html">ceil</a></div>
 <div class="up"><a href="ref.math.html">Math Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.bindec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">bindec</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">bindec</span> &mdash; <span class="dc-title">Binary to decimal</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.bindec-description">
  <h3 class="title">Description</h3>
   <div class="methodsynopsis dc-description">
    <span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span> <span class="methodname"><strong>bindec</strong></span>
     ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$binary_string</code></span>
    )</div>

  <p class="para rdfs-comment">
   Returns the decimal equivalent of the binary number represented by
   the <em><code class="parameter">binary_string</code></em> argument.
  </p>
  <p class="para">
    <span class="function"><strong>bindec()</strong></span> converts a binary number to an
   <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> or, if needed for size reasons, <span class="type"><a href="language.types.float.html" class="type float">float</a></span>.
  </p>
  <p class="para">
    <span class="function"><strong>bindec()</strong></span> interprets all
   <em><code class="parameter">binary_string</code></em> values as unsigned
   integers. This is because  <span class="function"><strong>bindec()</strong></span> sees
   the most significant bit as another order of magnitude
   rather than as the sign bit.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.bindec-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">binary_string</code></em></span>
     <dd>

      <p class="para">
       The binary string to convert
      </p>
     </dd>

    </dt>

   </dl>

  </p>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    The parameter must be a string.
    Using other data types will produce unexpected results.
   </p>
  </div>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.bindec-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The decimal value of <em><code class="parameter">binary_string</code></em>
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.bindec-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.1.0</td>
       <td>
        The function can now convert numbers that are too large to
        fit into the platforms <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type, larger values
        are returned as <span class="type"><a href="language.types.float.html" class="type float">float</a></span> in that case.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.bindec-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-3449">
    <p><strong>Example #1  <span class="function"><strong>bindec()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #DD0000">'110011'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #DD0000">'000110011'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #DD0000">'111'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
51
51
7
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3450">
    <p><strong>Example #2  <span class="function"><strong>bindec()</strong></span> interprets input as unsigned integers</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;The&nbsp;lesson&nbsp;from&nbsp;this&nbsp;example&nbsp;is&nbsp;in&nbsp;the&nbsp;output<br />&nbsp;*&nbsp;rather&nbsp;than&nbsp;the&nbsp;PHP&nbsp;code&nbsp;itself.<br />&nbsp;*/<br /><br /></span><span style="color: #0000BB">$magnitude_lower&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pow</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$magnitude_lower&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$magnitude_lower</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'See&nbsp;the&nbsp;rollover?&nbsp;&nbsp;Watch&nbsp;it&nbsp;next&nbsp;time&nbsp;around...'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_INT_MAX</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PHP_INT_MAX'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(~</span><span style="color: #0000BB">PHP_INT_MAX</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'interpreted&nbsp;to&nbsp;be&nbsp;one&nbsp;more&nbsp;than&nbsp;PHP_INT_MAX'</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'interpreted&nbsp;to&nbsp;be&nbsp;the&nbsp;largest&nbsp;unsigned&nbsp;integer'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'interpreted&nbsp;to&nbsp;be&nbsp;the&nbsp;largest&nbsp;unsigned&nbsp;integer<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(18446744073709551615)&nbsp;but&nbsp;skewed&nbsp;by&nbsp;float&nbsp;precision'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$note</span><span style="color: #007700">);<br /><br /><br />function&nbsp;</span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"input:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'%0'&nbsp;</span><span style="color: #007700">.&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bin&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"binary:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bin</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'precision'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;For&nbsp;readability&nbsp;on&nbsp;64&nbsp;bit&nbsp;boxes.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #0000BB">$bin</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'bindec():&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$dec&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$note</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"NOTE:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$note</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Output of the above example on 32 bit machines:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
input:        1073741823
binary:       00111111111111111111111111111111
bindec():     1073741823

input:        1073741824
binary:       01000000000000000000000000000000
bindec():     1073741824
NOTE:         See the rollover?  Watch it next time around...

input:        2147483647
binary:       01111111111111111111111111111111
bindec():     2147483647
NOTE:         PHP_INT_MAX

input:        -2147483648
binary:       10000000000000000000000000000000
bindec():     2147483648
NOTE:         interpreted to be one more than PHP_INT_MAX

input:        -1
binary:       11111111111111111111111111111111
bindec():     4294967295
NOTE:         interpreted to be the largest unsigned integer
</pre></div>
    </div>
    <div class="example-contents"><p>Output of the above example on 64 bit machines:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
input:        4611686018427387903
binary:       0011111111111111111111111111111111111111111111111111111111111111
bindec():     4611686018427387903

input:        4611686018427387904
binary:       0100000000000000000000000000000000000000000000000000000000000000
bindec():     4611686018427387904
NOTE:         See the rollover?  Watch it next time around...

input:        9223372036854775807
binary:       0111111111111111111111111111111111111111111111111111111111111111
bindec():     9223372036854775807
NOTE:         PHP_INT_MAX

input:        -9223372036854775808
binary:       1000000000000000000000000000000000000000000000000000000000000000
bindec():     9223372036854775808
NOTE:         interpreted to be one more than PHP_INT_MAX

input:        -1
binary:       1111111111111111111111111111111111111111111111111111111111111111
bindec():     18446744073709551616
NOTE:         interpreted to be the largest unsigned integer
              (18446744073709551615) but skewed by float precision
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.bindec-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.decbin.html" class="function" rel="rdfs-seeAlso">decbin()</a> - Decimal to binary</span></li>
    <li class="member"> <span class="function"><a href="function.octdec.html" class="function" rel="rdfs-seeAlso">octdec()</a> - Octal to decimal</span></li>
    <li class="member"> <span class="function"><a href="function.hexdec.html" class="function" rel="rdfs-seeAlso">hexdec()</a> - Hexadecimal to decimal</span></li>
    <li class="member"> <span class="function"><a href="function.base-convert.html" class="function" rel="rdfs-seeAlso">base_convert()</a> - Convert a number between arbitrary bases</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.base-convert.html">base_convert</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.ceil.html">ceil</a></div>
 <div class="up"><a href="ref.math.html">Math Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>