php method to convert a string into binary: 1. Use the bin2hex() function to convert the string into a hexadecimal value, the syntax is "bin2hex (string)"; 2. Use base_convert() to convert the 16 To convert a hexadecimal value to binary, the syntax is "base_convert(hexadecimal value,16,2)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In PHP, strings cannot be converted directly into binary, but you can use hexadecimal to convert it.
First use the bin2hex() function to convert the string into hexadecimal
Then use the base_convert() function to convert the hexadecimal value is binary.
Implementation method:
<?php header('content-type:text/html;charset=utf-8'); $str="Hello"; $hex=bin2hex($str); echo "16进制值为:".$hex; $binary=base_convert($hex,16,2); echo "<br>2进制值为:".$binary; ?>
Description:
base_convert(number,frombase,tobase)
The function can convert numbers between any bases.
Parameter | Description |
---|---|
number | Required. Specifies the number to be converted. |
frombase | Required. Specifies the original base of the number. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35. |
tobase | Required. Specifies the base to be converted. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35. |
Recommended study: "PHP Video Tutorial"
The above is the detailed content of php if convert string to binary. For more information, please follow other related articles on the PHP Chinese website!