Example, convert decimal to binary.
-
- //php implements base conversion
- function dec2bin ($dec) {
- $flag = array();
- while ($dec != 0) {
- array_push($flag, $dec%2);
- $dec = (int)($dec/2);
- }
- $binstr = '';
- while (!emptyempty($flag)) {
- $binstr .= array_pop($flag) ;
- }
- return $binstr;
- }
-
- echo dec2bin(7);
Copy code Note:
The above code is used to practice base conversion.
PHP has provided built-in functions decbin() and base_convert();
Example:
echo ' ';
- echo base_convert(7,10,2);
-
- echo '
';
-
- echo base_convert(1111,2,8);
- echo '
';
echo decbin(6);
-
Copy code
|