After UTF-8 is converted to GBK, the question mark "?" appears "?" Cause

Source: Internet
Author: User

The question mark "? "Reason

Webpage encoding conversion is the first step for parsing and processing webpages.

More and more web pages on the Internet use UTF-8 encoding, UTF-8 with 1 to 6 bytes encoded Unicode characters, contains more than 0.1 million characters, BMP part also has more than 60 thousand characters

 

During encoding conversion, we often need to convert to GBK encoding for subsequent processing. After conversion, many webpages will find a large number of consecutive question marks :????????

These disgusting question marks are introduced in the encoding conversion phase because:

 

The GBK character set contains more than 20 thousand characters, much less than the number of characters in the UTF-8.

When converting to GBK encoding, the encoding will fall beyond the GBK character set and cannot be converted to GBK encoding. These characters are converted '? '

 

UTF-8: Extended bytes (1 ASCII, 2 Greek letters and typographical characters, 3 Chinese characters and other multi-byte East Asian language, 4-plane symbols and special symbols, etc ),

Some of the double byte characters are not in the GBK character set, usually the characters that cannot be recognized by the UTF-8 are very uncommon, almost difficult to encounter, do not consider; but there is a character very special:C2a0

 

C2a0 is the formatting space in utf8 (different from the space of ASI = 20). This special character (UNICODE serial number is 0xa0) is not in the GBK character set. But it is frequently used in XML, HTML, and other formats. A large number of UTF-encoded web pages use this character as a placeholder space. In addition, different browsers use different processing methods: IE recognizes the symbol and displays it with spaces. Firefox replaces it with the XML Escape Character & nbsp; when the webpage uses c2a0 for text formatting, when we convert the webpage encoding to GBK, there will be many "? "Question mark

 

A simple solution is to filter webpages before transcoding:

If (code_type = "utf8 ")

{

STR = Str. Replace ('c2a0 ','');

}

 

 

 

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.