php method to convert utf8 to unicode: first extract 0100 in the first byte; then shift the result to the left by 12 bits; then extract 111101, shift the result to the left by 6 bits and The result obtained by the highest byte is ORed; finally, by analogy, the nth bit is directly ANDed with 111111 [0x3F].
php method to convert utf8 to unicode:
Of course, the conversion from UTF-8 to Unicode is also done through migration What is done by bits and so on is to extract the binary numbers at the corresponding positions in the UTF-8 format.
In the example "you" is three bytes, so each byte must be processed, from high bit to low bit. In UTF-8 "you" is 11100100,10111101,10100000. Starting from the high bit, that is, the first byte 11100100 is to take out the "0100". This is very simple. Just take the AND (&) with 11111 (0x1F). From the three bytes, we can know that the highest position must be before the 12th bit. , because six digits are taken each time. Therefore, the obtained result needs to be shifted to the left by 12 bits, and the highest bit is now 0100,000000,000000.
The second bit is to take out "111101", so you only need to AND (&) the second byte 10111101 and 111111 (0x3F). After shifting the result to the left by 6 bits and taking the result of the highest byte or (|), the second bit is completed, and the result is 0100,111101,000000. By analogy, the last digit is directly ANDed (&) with 111111 (0x3F), and then ORed (|) with the previous result to get the result 0100,111101,100000.
/** * utf8字符转换成Unicode字符 * @param [type] $utf8_str Utf-8字符 * @return [type] Unicode字符 */ function utf8_str_to_unicode($utf8_str) { $unicode = 0; $unicode = (ord($utf8_str[0]) & 0x1F) << 12; $unicode |= (ord($utf8_str[1]) & 0x3F) << 6; $unicode |= (ord($utf8_str[2]) & 0x3F); return dechex($unicode); } /** * Unicode字符转换成utf8字符 * @param [type] $unicode_str Unicode字符 * @return [type] Utf-8字符 */ function unicode_to_utf8($unicode_str) { $utf8_str = ''; $code = intval(hexdec($unicode_str)); //这里注意转换出来的code一定得是整形,这样才会正确的按位操作 $ord_1 = decbin(0xe0 | ($code >> 12)); $ord_2 = decbin(0x80 | (($code >> 6) & 0x3f)); $ord_3 = decbin(0x80 | ($code & 0x3f)); $utf8_str = chr(bindec($ord_1)) . chr(bindec($ord_2)) . chr(bindec($ord_3)); return $utf8_str; }
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to convert utf8 to unicode in php?. For more information, please follow other related articles on the PHP Chinese website!