decimal-to-binary, octal, hex-less digits front 0 _php Tutorial

Source: Internet
Author: User
Tags decimal to binary
/**
* decimal-to-binary, octal, hex-less digits 0 *
*
* @param array $datalist incoming data array (100,123,130)
* The binary @param int $bin conversion can be: 2,8,16
* @return Array return data array () returns no data conversion format
* @copyright Chengmo qq:8292669
*/
function Decto_bin ($datalist, $bin)
{
Static $arr =array (0,1,2,3,4,5,6,7,8,9, ' A ', ' B ', ' C ', ' d ', ' e ', ' f ');
if (!is_array ($datalist)) $datalist =array ($datalist);
if ($bin ==10) return $datalist; Same binary Ignore
$bytelen =ceil (16/$bin); Get the length of a byte if it is a $bin binary
$aoutchar =array ();
foreach ($datalist as $num)
{
$t = "";
$num =intval ($num);
if ($num ===0) continue;
while ($num >0)
{
$t = $arr [$num% $bin]. $t;
$num =floor ($num/$bin);
}
$tlen =strlen ($t);
if ($tlen% $bytelen!=0)
{
$pad _len= $bytelen-$tlen% $bytelen;
$t =str_pad ("", $pad _len, "0", str_pad_left). $t; Less than one byte length, auto front add 0
}
$aoutchar []= $t;
}
return $aoutchar;
}

http://www.bkjia.com/PHPjc/444835.html www.bkjia.com true http://www.bkjia.com/PHPjc/444835.html techarticle PHP Tutorial/** * Decimal to Binary, octal, hex less than 0 * * * @param array $datalist incoming data array (100,123,130) * @param int $bin Conversion of the binary ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.