Home > Article > Backend Development > How to correctly handle Chinese garbled characters caused by hexadecimal conversion in PHP
Title: Solve the problem of Chinese garbled characters caused by hexadecimal conversion in PHP
In the process of using PHP for hexadecimal conversion, sometimes you will encounter Chinese characters Garbled characters. This problem is usually caused by incorrect processing of Chinese encoding when performing hexadecimal conversion. This article will introduce how to correctly handle Chinese garbled characters caused by hexadecimal conversion in PHP, and provide specific code examples.
1. Problem Analysis
In PHP, we can use the two functions bin2hex
and hex2bin
to convert binary and hexadecimal conversion between. However, when processing Chinese characters, if the character encoding is not processed correctly, it will easily lead to the problem of Chinese garbled characters. This is because Chinese characters are usually multi-byte encoded. If Chinese characters are directly converted into hexadecimal, garbled characters will appear when converted back.
2. Solution
In order to correctly handle the hexadecimal conversion of Chinese characters, we can first convert the Chinese characters into UTF-8 encoded binary data, and then perform the hexadecimal conversion . When restoring, you need to first restore the hexadecimal data to binary data, and then convert the UTF-8 encoded binary data into Chinese characters.
The specific code is as follows:
// 将中文字符转换为UTF-8编码的二进制数据 function utf8_str_to_bin($str){ $arr = preg_split('/(?<!^)(?!$)/u', $str); $bin_str = ''; foreach($arr as $val){ $bin_str .= pack("H*", bin2hex(mb_convert_encoding($val, 'UTF-16', 'UTF-8'))); } return $bin_str; } // 将UTF-8编码的二进制数据转换为中文字符 function bin_to_utf8_str($bin_str){ $str = ''; $length = strlen($bin_str); for($i = 0; $i < $length; $i++){ if($bin_str[$i] === '\' && $bin_str[$i + 1] === 'x'){ $hex = substr($bin_str, $i + 2, 2); $str .= mb_convert_encoding(pack('H*', $hex), 'UTF-8', 'UTF-16'); $i += 3; }else{ $str .= $bin_str[$i]; } } return $str; } // 示例 $chinese_str = "你好"; $bin_data = utf8_str_to_bin($chinese_str); $hex_data = bin2hex($bin_data); echo "原始中文字符:".$chinese_str."<br>"; echo "中文字符转二进制数据:".$bin_data."<br>"; echo "二进制数据转16进制数据:".$hex_data."<br>"; $bin_data_back = hex2bin($hex_data); $chinese_str_back = bin_to_utf8_str($bin_data_back); echo "还原中文字符:".$chinese_str_back;
Through the above code examples, we can correctly handle the Chinese garbled problem caused by hexadecimal conversion in PHP. I hope this article can help developers with similar problems so that Chinese characters will no longer appear garbled during hexadecimal conversion.
The above is the detailed content of How to correctly handle Chinese garbled characters caused by hexadecimal conversion in PHP. For more information, please follow other related articles on the PHP Chinese website!