Home  >  Article  >  Backend Development  >  php if convert string to binary

php if convert string to binary

青灯夜游
青灯夜游Original
2022-04-21 20:23:123852browse

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

php if convert string to binary

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

php if convert string to 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!

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