Recognition of unicode and utf8 encoding

Source: Internet
Author: User
: This article mainly introduces unicode and utf8 encoding. if you are interested in the PHP Tutorial, refer to it. Recognition of unicode and utf8 encoding

Until today, to be accurate, just now, I know that UTF-8 encoding and Unicode encoding are different, there is a differenceBytes
There is a certain relationship between them. look at them, but don't:

The length of the UTF-8 is not certain, may be 1, 2, 3 bytes Unicode length is certain, 2 bytes (USC-2) UTF-8 can and Unicode conversion
Unicode and utf8
Unicode (hexadecimal) UTF-8 (binary)
0000-007F 0 xxxxxxx
0080-07FF 110 xxxxx 10 xxxxxx
0800-FFFF 1110 xxxx 10 xxxxxx 10 xxxxxx

The above table has 2 meanings, the first obvious is Unicode and UTF-8 character range correspondence, there is a can see how Unicode and UTF-8 conversion:

First let's talk about the conversion from UTF-8 to Unicode

The binary code of the UTF-8 matches with the above three formats, removes the fixed bit after matching (non-x position in the table), and then a group of every 8 digits from right to left, not 8 bits left not collar, enough 2 bytes 16 bits, the 16 bits represents the Unicode encoding corresponding to the UTF-8, take a look at the following example:

The text encoding format in the above picture is UTF-8, you can use WinHex to see its hexadecimal representation

Character => UTF-8 => UTF-8 binary => remove the fixed position to make up 16-bit binary => 16 hexadecimal Chinese => E6B189 => 11100110 10110001 10001001 => 01101100 01001001 => 6C49 Chinese => E5AD97 => 11100101 10101101 10010111 => 0101101101010111 => 5B57 # The following is the result of running '\ u6C49' "Han" '\ u5B57 "# here, conversion from UTF-8 to Unicode is already a very easy thing to look at the conversion of pseudo code to read a byte, 11100110 to determine the format of the UTF-8 character, which belongs to the third, 3 bytes continue to read 2 bytes get 11100101 10101101 10010111 removed by format fixed bit 1011011 less than 16 bits, left fill 01010111 01011011 01010111 => 5B57
Let's look at the conversion from Unicode to UTF-8.
5B57 get the Unicode range where 5B57 is located, 0800 <= 5B57 <= FFFF, it is known that the UTF-8 of 5B57 has three bytes, format: 1110 xxxx 10 xxxxxx 10xxxxxx get 5B57 binary code 101101101010111 use the binary code in the previous step to splice UTF-8 code from right to left 11100101 10101101
Question

Let's talk about the cause of today's problem, from the front-end input a lot of words, UTF-8 format each word up to 30 bytes, so the front-end and the background are verified, javascript uses Unicode encoding, the back-end program uses UTF-8 encoding, and now the solution is

Front end
Function utf8_bytes (str) {var len = 0, unicode; for (var I = 0; I <str. length; I ++) {unicode = str. charCodeAt (I); if (unicode <0x0080) {++ len;} else if (unicode <0x0800) {len ++ = 2 ;} else if (unicode <= 0 xFFFF) {len + = 3;} else {throw "characters must be USC-2 !! "} Return len ;}# example utf8_bytes ('asdasdas ') 8utf8_bytes ('yrt Yan Ritao') 12
Background
# For GBK strings $ len = ceil (strlen (bin2hex (iconv ('gbk', 'utf-8', $ word)/2 ); # for UTF8 string $ len = ceil (strlen (bin2hex ($ word)/2 );

5/21/2015 8:21:53

The copyright of this article is owned by the author iforever (luluyrt@163.com), without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.

The above introduces unicode and utf8 encoding again, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.