an analysis of the principle of Chinese character to decimal
In GBK encoding, a Chinese character consists of two characters, and the method of acquiring Chinese character string is as follows
Copy CodeThe code is as follows:
$string = "Don't be infatuated with elder 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);
Since a Chinese character consists of two characters, obtaining the ASCII value of a character through the Ord () function, if it is greater than 127, determines that the current character is the first half of a Chinese character, and that the second half of the Chinese character is acquired. Of course, this method of judgment should be combined with a specific development environment, and if there is a single character with an ASCII value greater than 127, this method of judging is obviously incorrect.
The principle of the PHP implementation of the literal character to decimal is to get two characters of a Chinese character through A For loop method, and then use the Ord () function to convert each character to decimal. As above are: not [178 187] to [210 170] Fans [195 212] Love [193 181] elder brother [184 231]
Two, Chinese characters to hexadecimal principle analysis
Using the UltraEdit development tool, you can view the hexadecimal characters of the Chinese character directly, such as
For example, check the hex of five words "do not crush elder brother"
From the above figure can know each character pairs should be the hexadecimal characters are: no B2BB to D2AA c3d4 Love c1b5 brother B8e7
PHP implementation of the text character to hexadecimal principle is to first use the Ord () function to take out each Chinese characters of the decimal, specifically to see [the PHP Function Chapter Mastering ord () and Chr () function application], and then use the Dechex () function to convert each Chinese character to hexadecimal
Instance source code
Copy CodeThe code is as follows:
$string = "Don't be infatuated with elder 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);
Results such as
Using the method above to convert Chinese characters to 16, the output can be compared with the hex obtained by the UltraEdit development tool.
three, Chinese characters to binary and octal principle analysis
The implementation of Chinese characters to binary and octal with the above 16 binary conversion principle, just the conversion function is different, combined with the above example code, to achieve the following
Chinese characters go binary, as follows
Copy CodeThe code is 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:
Chinese characters to octal, the method is as follows
Copy CodeThe code is 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 text character in the conversion principle, and then through the PHP built-in function UrlDecode () can be used to convert the hexadecimal string into normal Chinese characters, please pay attention to the next installment of the text character encoding Study series of UrlDecode () and UrlEncode () The encoding principle of the function number character.
http://www.bkjia.com/PHPjc/324690.html www.bkjia.com true http://www.bkjia.com/PHPjc/324690.html techarticle One, the Chinese characters to the decimal principle analysis GBK code in a character consists of two characters, the method of acquiring Chinese character string is as follows: $string = "Don't be infatuated with brother ...