PHP implementation of the conversion (binary, octal, hexadecimal) conversion to achieve code _php skills

Source: Internet
Author: User
Tags decimal to binary php language
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 Code code 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;
}

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 decimal
This conversion uses multiplication, such as: 1101 to decimal: 1*2^3+1*2^2+0*2^1+1*2^0
Code:

Copy Code code as follows:

<?php
/**
* Binary, octal, hexadecimal decimal *
*
* @param array $datalist incoming data array (DF,EF)
* @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 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; is 10-in-process without conversion
$aOutData =array (); Define output Save Array
foreach ($datalist as $num)
{
$atnum =str_split ($num); To split 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 (' ff ', ' ff33 ', ' cc33 '), 16);
Var_dump (Bin_todec (' 1101101 ', ' 111101101 '), 2));
Var_dump (Bin_todec (' 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 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!

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.