Home  >  Article  >  Backend Development  >  How to convert string to binary in php

How to convert string to binary in php

青灯夜游
青灯夜游Original
2022-06-13 17:19:332737browse

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)".

How to convert string to binary in php

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>";
?>

How to convert string to binary in php

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(&#39;content-type:text/html;charset=utf-8&#39;);  
$str = bin2hex("Hello");
echo "字符串对应的16进制值:".$str."<br>";
echo "字符串对应的2进制值:".base_convert($str, 16, 2);
?>

How to convert string to binary in php

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!

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