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:43 2832browse

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:

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.

"; echo bin2hex("123")."
"; echo bin2hex("10")."
"; ?>

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:

"; echo dechex("10") . "
"; echo dechex("1587") . "
"; 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 parametertobaseis 16, other base numbers can be converted into hexadecimal numbers.

Example:

"; echo base_convert("364", 8, 16) . "
"; ?>

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