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

How to convert hexadecimal to string function in php

PHPz
PHPzOriginal
2023-04-19 09:15:181118browse

As a server-side programming language, PHP has a rich function library to facilitate developers to develop quickly. PHP provides a function for converting hexadecimal to string. Let’s introduce this function below.

The function that converts hexadecimal to string in PHP is hex2bin(). Its function is to convert hexadecimal numbers into strings.

Sample code:

$string = hex2bin('546869732069732061207465737420737472696e67');
echo $string;

Run result:

This is a test string

In the above example, the function hex2bin() converts hexadecimal The string '546869732069732061207465737420737472696e67' is converted into the string 'This is a test string'. As you can see, the converted string is the same as the original hexadecimal string.

It should be noted that if the hexadecimal number passed in is not an even number of digits (that is, not divisible by 2) when calling the hex2bin() function, PHP will automatically add a ' at the end of the string 0' character, so that it becomes an even number, and then converted.

Sample code:

$string = hex2bin('546869732069732061207465737420737472696e670');
echo $string;

Run result:

This is a test string

Hexadecimal string '546869732069732061207465737420737472696e670' at the end The character '0' is added automatically by PHP.

In addition to the hex2bin() function, there are the following two functions in PHP that can convert hexadecimal numbers into strings:

  1. pack() function:
    pack () function can be used to convert hexadecimal numbers into strings. Unlike the hex2bin() function, the pack() function can convert according to different formats.

Format description:

H - Hexadecimal string (high endian)
h - Hexadecimal string (low endian)
N - unsigned long (network byte order)
n - unsigned short (network byte order)
V - unsigned long (native byte order)
v - unsigned Short integer (local byte order)

Sample code:

$string = pack("H*", '546869732069732061207465737420737472696e67');
echo $string;

Running result:

This is a test string

This code is the same as hex2bin( ) function is used similarly. The difference is that the pack() function can parse hexadecimal strings according to different formats.

  1. chr() function: The
    chr() function is used to convert the specified ASCII code value into characters. Because one byte can represent 256 different characters, the hexadecimal number of one byte corresponds to an ASCII code value. Therefore, by splitting the hexadecimal string into two character groups and converting each group into its corresponding ASCII code value, the converted string can be obtained.

Sample code:

$str = "546869732069732061207465737420737472696e67";
$len = strlen($str);
$res = "";
for ($i = 0; $i < $len; $i += 2) {
    $res .= chr(hexdec(substr($str, $i, 2)));
}
echo $res;

Running result:

This is a test string

In the above example, we first add sixteen The base string $str is split into two characters as a group, so the length of $str is an even number. Iterate through each group through a for loop, call the hexdec() function to convert it into a decimal number, then call the chr() function to convert it into the corresponding character, and add the character to the $res string.

To sum up, PHP provides a variety of methods to convert hexadecimal numbers into strings. Developers can choose the appropriate method according to actual needs.

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