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 Code2, 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:
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; For 10 binary does not convert $aOutData =array (); Define output Save array foreach ($datalist as $num) {$atnum =str_split ($num);//Divide the 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 CodeSummary: In PHP built in a number of functions can be completed in the conversion of the above, such as Bindec (), Decoct (), Dechex () Base_convert () Decbin () and so on. Through this article, bring us a realization of PHP in the conversion of the idea, I hope to be helpful to everyone. |