php steps to convert string (string) to binary: 1. Use bin2hex() function to convert the string to hexadecimal value, syntax "bin2hex(string)"; 2. Use base_convert() The function can convert a hexadecimal value into a binary value, the syntax is "base_convert(hexadecimal value, 16, 2)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php will string ( Method to convert string) to binary
#php converts string (string) to binary, you need to use hexadecimal.
Step 1: Use the bin2hex() function to convert string (string) to hexadecimal
bin2hex(string)
The function converts ASCII Converts a string of characters to a hexadecimal value.
Return value: Returns the hexadecimal value of the string to be converted.
<?php $str = bin2hex("Hello!"); echo "字符串对应的16进制值:".$str."<br>"; ?>
Step 2: Use the base_convert() function to convert the hexadecimal value to a binary value
base_convert( The number or string to be converted, the original base, the base to be converted)
function, it can convert between any bases
Just need to setbase_convert(hexadecimal System value, 16, 2)
can convert the hexadecimal value to binary.
<?php header('content-type:text/html;charset=utf-8'); $str = bin2hex("Hello"); echo "字符串对应的16进制值:".$str."<br>"; echo "字符串对应的2进制值:".base_convert($str, 16, 2); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert string to binary in php. For more information, please follow other related articles on the PHP Chinese website!