Home  >  Article  >  Backend Development  >  How to change Chinese to hexadecimal in php

How to change Chinese to hexadecimal in php

PHPz
PHPzOriginal
2023-04-24 10:50:311568browse

PHP is a popular programming language that is widely used in the field of web development. During the development process, it is sometimes necessary to convert Chinese text into hexadecimal for easy storage, transmission and processing. This article will introduce how to use PHP to realize the function of converting Chinese to hexadecimal.

1. What is hexadecimal

In computers, numbers are stored and calculated in the form of binary (composed of 0 and 1). Hexadecimal is a representation of binary numbers, using 16 characters (0-9, A-F) to represent 16 numbers (0-15).

For example, the hexadecimal number corresponding to the decimal number 10 is A, and the hexadecimal number corresponding to the decimal number 255 is FF.

2. Convert Chinese to hexadecimal

In PHP, you can use the bin2hex() function to convert a string into hexadecimal. However, this function can only convert English letters, numbers and some symbols into hexadecimal, and cannot directly convert Chinese into hexadecimal.

In order to solve this problem, we can use the byte-by-byte conversion method of UTF-8 encoding to convert Chinese characters into hexadecimal representation one by one. The specific method is as follows:

$string = "Chinese string";
$hex = '';
for ($i = 0; $i < mb_strlen ($string); $i ) {

$hex .= sprintf("%02X", ord(mb_substr($string, $i, 1)));

}
echo $hex; // Output "E4B8ADE69687E5AD97E7ACACE4B8B2E7A792E7958C"
?>

In the above code, we first define Get a string containing Chinese characters, then use a loop to obtain the UTF-8 encoded byte sequence byte by byte, and use the sprintf() function to convert each byte into a two-digit hexadecimal representation. Finally, by splicing all the hexadecimal representations together, the hexadecimal representation of the Chinese string is obtained.

3. Convert hexadecimal to Chinese

Similarly, we can also use the hex2bin() function to convert hexadecimal into a string. However, since Chinese characters are usually stored and transmitted in the form of UTF-8 encoding, when converting hexadecimal to Chinese characters, you need to first convert the hexadecimal to a UTF-8 encoded byte sequence, and then Use the mb_convert_encoding() function to convert a sequence of bytes into a string.

The specific method is as follows:

$hex = "E4B8ADE69687E5AD97E7ACACE4B8B2E7A792E7958C";
$string = '';
for ($i = 0; $ i < strlen($hex); $i = 2) {

$string .= chr(hexdec(substr($hex, $i, 2)));

}
$string = mb_convert_encoding($string, "UTF-8", "GBK");
echo $ string; // Output "Chinese string"
?>

In the above code, we first define a hexadecimal string, and then use a loop to obtain the 16 bytes of each byte one by one Base representation, convert it to the corresponding ASCII code, and then use the chr() function to convert the ASCII code into characters. Note that since Chinese characters are usually stored and transmitted in GBK encoding, when converting a byte sequence into a string, you need to set the target encoding to UTF-8.

4. Summary

This article introduces how to use PHP to convert Chinese to hexadecimal and convert hexadecimal to Chinese. It should be noted that when converting hexadecimal to Chinese, you need to first convert the hexadecimal to a UTF-8 encoded byte sequence and decode it in UTF-8 encoding. I hope this article can help developers process Chinese text more conveniently.

The above is the detailed content of How to change Chinese to hexadecimal 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