Decimal converts to binary, octal, hexadecimal
From decimal to other transformations, the remainder is read by dividing the number into the number of numbers that are being converted. The connection is OK.
Copy CodeThe code is as follows:
<?php
/**
* Decimal to binary, octal, hexadecimal low digits front complement 0 *
*
* @param array $datalist incoming data array (100,123,130)
* @param int $bin conversion can be: 2,8,16
* @return Array return data array () returns a format without data conversion
* @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 system Ignore
$bytelen =ceil (16/$bin); To get the length of a byte if it is $bin
$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, automatic front complement 0
}
$aOutChar []= $t;
}
return $aOutChar;
}
x-powered-by:php/5.2.0
Content-type:text/html
Array (3) {
[0]=>
Int (255)
[1]=>
Int (65331)
[2]=>
Int (52275)
}
Array (2) {
[0]=>
Int (124)
[1]=>
Int (508)
}
Array (2) {
[0]=>
Int (342099)
[1]=>
Int (5345)
}
Something, these are just the implementation of the way, in fact, do not care about the PHP language or other, the realization of ideas are the same. PHP actually has a lot of built-in functions to do this:
Bindec (), Decoct (), Dechex () Base_convert () Decbin () This is just a way to implement it. Oh!
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.