Home  >  Article  >  Backend Development  >  How to convert hexadecimal value into string in php

How to convert hexadecimal value into string in php

青灯夜游
青灯夜游Original
2022-08-31 19:48:432952browse

In PHP, you can use the hex2bin() function to convert hexadecimal values ​​into strings. The function of this function is to convert hexadecimal values ​​into ASCII-encoded character values, that is, strings , the syntax is "hex2bin (hexadecimal value)"; if the conversion is successful, the corresponding string is returned, if it fails, FALSE is returned.

How to convert hexadecimal value into string in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

In php, You can use the hex2bin() function to convert hexadecimal values ​​to strings.

hex2bin() function can convert hexadecimal values ​​into ASCII-encoded character values, that is, strings.

Syntax:

hex2bin($string)
  • $string: Specifies the hexadecimal value to be converted and cannot be omitted.

Return value: Returns the ASCII character value of the converted string, or FALSE if it fails.

Example:

<?php
$hexstr="48656c6c6f20576f726c6421";
$str=hex2bin($hexstr);
echo $str;
?>

Output:

How to convert hexadecimal value into string in php

Explanation:

The opposite effect of the hex2bin() function is the bin2hex() function.

bin2hex() function converts a string of ASCII characters into a hexadecimal value.

bin2hex(string)

Return value: Returns the hexadecimal value of the string to be converted.

<?php
echo bin2hex("Hello!")."<br>";
echo bin2hex("123")."<br>";
echo bin2hex("10")."<br>";
?>

How to convert hexadecimal value into string in php

Extended knowledge: Convert other base data into hexadecimal strings

1. dechex() function Conversion

dechex() function converts a decimal number to a hexadecimal number.

dechex(number);

Return value: a string containing a hexadecimal number with a decimal value.

Example:

<?php
echo dechex("30") . "<br>";
echo dechex("10") . "<br>";
echo dechex("1587") . "<br>";
echo dechex("70");
?>

How to convert hexadecimal value into string in php

2. base_convert() function conversion

base_convert() function converts numbers between arbitrary bases.

base_convert(number,frombase,tobase);
Parameters 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.

When the value of parameter tobase is 16, other base numbers can be converted into hexadecimal numbers.

Example:

<?php
echo base_convert("30", 10, 16) . "<br>";
echo base_convert("364", 8, 16) . "<br>";
?>

How to convert hexadecimal value into string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert hexadecimal value into string 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