/**
- * 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
- * @Author Chengmo qq:8292669
- * @copyright Http://www.cnblogs.com/chengmo
- */
- 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"
- }
Copy CodeBinary, 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
/**
- * 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)
- }
Copy CodePHP actually has a few built-in functions to do this: Bindec (), Decoct (), Dechex () Base_convert () Decbin () Here is just the idea of implementation. |