PHP implements a binary conversion (binary, octal, hex) mutual Conversion Implementation Code _php tutorial

Source: Internet
Author: User
Decimal conversion to binary, octal, hexadecimal
From decimal to other binary conversions, the remainder is read by dividing the number by the number of binary numbers to be converted. Connected together on it.
Copy CodeThe code is as follows:
/**
* 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;
}

Test:
Var_dump (Decto_bin (Array (128,253), 2));
Var_dump (Decto_bin (Array (128,253), 8));
Var_dump (Decto_bin (Array (128,253), 16));

x-powered-by:php/5.2.0
Content-type:text/html
Array (2) {
[0]=>
String (8) "10000000"
[1]=>
String (8) "11111101"
}
Array (2) {
[0]=>
String (4) "0200"
[1]=>
String (4) "0375"
}
Array (2) {
[0]=>
String (2) "80"
[1]=>
String (2) "FD"
}
Binary, octal, hexadecimal to decimal
This conversion is multiplied, for example: 1101 to decimal: 1*2^3+1*2^2+0*2^1+1*2^0
Code:

Copy CodeThe code is as follows:
/**
* Binary, octal, hexadecimal to decimal *
*
* @param array $datalist incoming data array (DF,EF)
* 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 Bin_todec ($datalist, $bin)
{
Static $arr =array (' 0 ' =>0, ' 1 ' =>1, ' 2 ' =>2, ' 3 ' =>3, ' 4 ' =>4, ' 5 ' =>5, ' 6 ' =>6, ' 7 ' =>7, ' 8 ' =>8, ' 9 ' =>9, ' A ' =>10, ' B ' =>11, ' C ' =>12, ' D ' =>13, ' E ' =>14, ' F ' =>15 ';
if (!is_array ($datalist)) $datalist =array ($datalist);
if ($bin ==10) return $datalist; No conversion for 10 binary
$aOutData =array (); Define output Save Array
foreach ($datalist as $num)
{
$atnum =str_split ($num); Splitting a string into a single character array
$atlen =count ($atnum);
$total = 0;
$i = 1;
foreach ($atnum as $TV)
{
$tv =strtoupper ($TV);
if (Array_key_exists ($TV, $arr))
{
if ($arr [$tv]==0) continue;
$total = $total + $arr [$tv]*pow ($bin, $atlen-$i);
}
$i + +;
}
$aOutData []= $total;
}
return $aOutData;
}

Test:
Var_dump (Bin_todec (Array (' FF ', ' ff33 ', ' cc33 '), 16));
Var_dump (Bin_todec (Array (' 1101101 ', ' 111101101 '), 2));
Var_dump (Bin_todec (Array (' 1234123 ', ' 12341 '), 8));

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 only the implementation of the way, in fact, do not care PHP language or other, the realization of ideas are the same. PHP actually has a few built-in functions to do this:
Bindec (), Decoct (), Dechex () Base_convert () Decbin () Here is just the idea. Oh!

http://www.bkjia.com/PHPjc/322507.html www.bkjia.com true http://www.bkjia.com/PHPjc/322507.html techarticle decimal is converted to binary, octal, hexadecimal from decimal to other binary, with the number being divided by the number of bytes to be converted, and the remainder is read. Connect together to ...

  • 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.