_php techniques for analyzing the principle of character conversion in PHP

Source: Internet
Author: User
One, the Chinese character to decimal principle analysis

In GBK encoding, a Chinese character is composed of two characters, and the method to get the character string is as follows
Copy Code code as follows:

$string = "Don't be infatuated with brother";
$length = strlen ($string);
for ($i =0; $i < $length; $i + +) {
if (Ord ($string [$i]) >127) {
$result [] = Ord ($string [$i]). ' '. Ord ($string [+ + $i]);
}
}
Var_dump ($result);


Because a Chinese character is composed of two characters, the ASCII value of a character is obtained by the Ord () function if it is greater than 127, it is possible to determine that the current character is the first half of a character, and to obtain the second half of the Chinese character. Of course, this method of judgment should be combined with a specific development environment, if there is a single character with an ASCII value greater than 127, this approach is clearly incorrect.

The principle of the PHP implementation of the character decimal is to get the two characters of a Chinese character by means of a for loop, and then use the Ord () function to convert the characters to decimal. As above are: not [178 187] to [210 170] Fans [195 212] Love [193 181] brother [184 231]

Two, the analysis of Chinese character to hexadecimal principle

Using the UltraEdit development tool, you can see the hexadecimal of the Chinese character directly, as shown below

For example, look at the "Don't Crush on brother" five-word hexadecimal

From the above figure can know each Chinese character pairs should be hexadecimal characters are: not B2BB to D2AA fan c3d4 love c1b5 brother B8e7

PHP implementation of the principle of character to hexadecimal is the first use of the Ord () function to take out the decimal of the Chinese characters, specific to see [PHP function Chapter Master ord () and Chr () function application], and then use the Dechex () function to convert the characters to hexadecimal

Instance source code
Copy Code code as follows:

$string = "Don't be infatuated with brother";
$length = strlen ($string);
Echo $string;
$result = Array ();
Decimal
for ($i =0; $i < $length; $i + +) {
if (Ord ($string [$i]) >127) {
$result [] = Ord ($string [$i]). ' '. Ord ($string [+ + $i]);
}
}
Var_dump ($result);
Hexadecimal
$strings = Array ();
foreach ($result as $v) {
$dec = Explode ("", $v);
$strings [] = Dechex ($dec [0]). " ". Dechex ($dec [1]);
}
Var_dump ($strings);

The result is the following figure

By using the above method to convert the Chinese character to 16, the output can be compared with the hexadecimal obtained by the UltraEdit development tool.

analysis on the principle of binary and octal in Chinese characters

The implementation of the binary and octal with the above 16 conversion principle, but the transformation of the function is different, combined with the above instance code, to achieve the following

The Chinese character is binary, as follows
Copy Code code as follows:

$strings = Array ();
foreach ($result as $v) {
$dec = Explode ("", $v);
$strings [] = Decbin ($dec [0]). " ". Decbin ($dec [1]);
}
Var_dump ($strings);

The results are as follows:

The Chinese character is transferred to octal method as follows
Copy Code code as follows:

$strings = Array ();
foreach ($result as $v) {
$dec = Explode ("", $v);
$strings [] = decoct ($dec [0]). " ". DECOCT ($dec [1]);
}

The results are as follows:

Understand the PHP implementation of the character into the conversion principle, and then through the PHP built-in function urldecode () can convert the hexadecimal string into the normal Chinese characters through the combination, please pay attention to the next issue of the text Encoding Research series UrlDecode () and UrlEncode () The coding principle of the function number character.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.