Home > Backend Development > PHP Tutorial > Decimal to binary

Decimal to binary

巴扎黑
Release: 2016-11-12 13:04:38
Original
1316 people have browsed it

function dec2bin ($dec) {
    $flag = array();
    while ($dec != 0) {
         array_push($flag,$dec%2);
         $dec = (int)($dec/2);
    }
    $binstr = '';
    while (!empty($flag)) {
        $binstr .= array_pop($flag);
    }
    return $binstr;
}
echo dec2bin(7);
Copy after login

Note: The above is just for practice. PHP already has built-in functions decbin() and base_convert();

echo &#39;<br/>&#39;;
echo base_convert(7,10,2);
echo &#39;<br/>&#39;;
echo base_convert(1111,2,8);
echo &#39;<br/>&#39;;
echo decbin(6);
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template