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

How to convert hexadecimal value into string in php

青灯夜游
Release: 2023-03-16 19:16:02
Original
3207 people have browsed it

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)
Copy after login
  • $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;
?>
Copy after login

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)
Copy after login

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>";
?>
Copy after login

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);
Copy after login

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");
?>
Copy after login

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);
Copy after login
ParametersDescription
numberRequired . Specifies the number to be converted.
frombaseRequired. 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.
tobaseRequired. 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>";
?>
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template