Home  >  Article  >  Backend Development  >  How to convert decimal number to binary number in PHP

How to convert decimal number to binary number in PHP

PHPz
PHPzOriginal
2023-04-25 18:28:151185browse

In computer science, binary numbers are the most basic way of representing numbers and are also the number system used by computers. Converting a decimal number to a binary number is a common problem in computer science. PHP is a popular server-side scripting language that also provides some built-in functions to convert decimal numbers to binary numbers. This article will introduce how to convert decimal numbers to binary numbers using PHP.

1. Use PHP built-in functions

PHP provides a built-in function decbin() to convert decimal numbers to binary numbers.

The following is a sample code that uses the decbin() function to convert a decimal number to a binary number:

$decimal_number = 15;
$binary_number = decbin($decimal_number);

echo "十进制数 " . $decimal_number . " 转换为二进制数是:" . $binary_number;

Executing the above code will output the following results:

十进制数 15 转换为二进制数是:1111

In the above In the sample code, we first assign a decimal number 15 to the variable $decimal_number. We then pass $decimal_number to PHP’s built-in function decbin(). The decbin() function converts $decimal_number to a binary number and saves the result in the variable $binary_number.

Finally, we output the value of $binary_number to the screen. Combining the above steps, we successfully converted the decimal number 15 to the binary number 1111.

2. Manual calculation method

In addition to using PHP built-in functions, we can also manually convert decimal numbers to binary numbers. The way to manually calculate binary numbers is to keep dividing the decimal number by 2 until the result is 0. In each division operation, we need to record the remainder, and then arrange the remainder in reverse order to get the binary representation of the decimal number.

The following is a sample code for manually calculating decimal and binary numbers:

$decimal_number = 15;
$binary_number = "";

while($decimal_number>0){
  $reminder = $decimal_number%2;
  $binary_number .= $reminder;
  $decimal_number = $decimal_number/2;
}

$binary_number = strrev($binary_number);

echo "十进制数 " . $decimal_number . " 转换为二进制数是:" . $binary_number;

In the above sample code, we first assign a decimal number 15 to the variable $decimal_number. Then, use a while loop to convert the decimal number to binary.

In each iteration, we divide $decimal_number by 2 and store the remainder in the variable $reminder. Then, add the value of $reminder to the end of the variable $binary_number.

Finally, we use the PHP built-in function strrev() to invert $binary_number and assign it to the variable $binary_number.

Execute the above code and you will get the following results:

十进制数 15 转换为二进制数是:1111

3. Use arrays to solve the numerical order problem in manual calculation methods

In the process of manual calculation of binary numbers, we need Arrange the remainders in reverse order to get the correct binary number. In PHP, using arrays can solve this problem.

The following is a sample code that uses an array to sort the remainders in reverse order to convert a decimal number into a binary number:

$decimal_number = 15;
$binary_number = [];

while($decimal_number>0){
  $reminder = $decimal_number%2;
  array_push($binary_number,$reminder);
  $decimal_number = $decimal_number/2;
}

$binary_number = array_reverse($binary_number);
$binary_number = implode($binary_number);

echo "十进制数 " . $decimal_number . " 转换为二进制数是:" . $binary_number;

In the above sample code, we first assign a decimal number 15 to a variable $decimal_number. Then, use a while loop to convert the decimal number to binary.

In each iteration, we divide $decimal_number by 2 and store the remainder in the array $binary_number. Then, we use the PHP built-in function array_reverse() to sort the $binary_number array in reverse order. Finally, we use the implode() function to convert the $binary_number array into a string.

Execute the above code and you will get the following results:

十进制数 15 转换为二进制数是:1111

Summary:

This article introduces how to convert decimal numbers to binary numbers in PHP. We can use PHP's built-in function decbin() to convert decimal numbers to binary numbers. In addition, we can also manually calculate binary numbers, arrange the remainder in reverse order to solve the numerical order problem, or use an array to store the remainder and arrange it in reverse order to get the correct binary number. Of course, the easiest way is to use PHP's built-in function decbin() function.

The above is the detailed content of How to convert decimal number to binary number in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn