Caused by adding Chinese characters to the intermediate characters "\" in the cstring type .....

Source: Internet
Author: User

Various messages about Microsoft's lack of MFC maintenance make many people no longer use MFC as their own software development framework. However, I feel that MFC has brought great convenience to software development, especially for those who come from Computer Science and Technology Division, so now I am still a loyal user of MFC.

Solve the problem of Chinese character storage in cstring today:

Question: read an image from the hard disk. The absolute address of the image exists in the cstring variable. Use the value assignment constructor cstring grapath; grapath = "F: \ Image Library \ A. tif ". However, the path name displayed in the afxmessagebox of MFC is a bunch of garbled characters.

Explanation: This is an interesting question. Theoretically, I can solve this problem by using the _ T ("") specification for the string used for initialization. However, this is counterproductive. So I tried using this string: grapath = "Image Library \. TIF ", the string so displayed is normal, and then I use this string: grapath =" F: Image Library \. TIF ", everything is normal, So I focused on. A mix of Chinese characters and English letters produces an error. It turns out to be an escape character. Next, let's take a look at this familiar and unfamiliar escape character (escape sequence ).

In order for netizens who encounter the same problem to find a solution to the problem in the first place, I will first put the following conclusions:

The cstring is represented in ASCII and Unicode. Each English letter occupies one byte, and the Chinese character occupies two byte, And the Escape Character "\" and its subsequent letters constitute one byte. In addition to the specified definition, you cannot add "\" to the string.

That's simple.

Therefore, the following program extracts Chinese characters and English characters separately from strings is very obvious. But don't forget our topic today: escape characters. I am more concerned with the position of escape characters in the entire string.

The Escape Character belongs to the ASCII category. The Escape Character "\" has the meaning of escape only when it is combined with a specific letter. However, there are two errors when using the escape character:

① "\" Is not followed by the specified letter

② "\" Followed by Chinese characters, as I did.

The following is a one-to-one analysis. If the specified letter is not followed, for example, we initialize the following string: cstring SSS = "\ U hello ". \ U does not have corresponding values in the ASCII table. In this case, what results will be produced when we call the printf function to output the string? The printf function will ignore '\' and directly output "U hello ". That is, the string is actually saved as u ASCII in the memory.

In the second case, We initialize the following string: cstring SSS = "\ hello ". The output result of the string output by calling the printf function is: ello. After many experiments, we found that Unicode encoding will eat the first ASCII code, which is between the complexity of Unicode encoding. We just need to remember a simple conclusion: do not add Chinese characters After escape characters.

# Include "stdafx. H "# include <afx. h> cstring getchinese (const cstring & DST); cstring getenglish (const cstring & DST); int main (INT argc, char * argv []) {// cstring SSS = _ T ("ha: \ A \ hello, what is he"); cstring SSS = "China haha "; cstring SCHN = getchinese (SSS); cstring Seng = getenglish (SSS); printf ("% s \ n", SCHN); printf ("% s \ n", Seng ); // cout <SSS <Endl; return 0;} cstring getchinese (const cstring & DST) {int I; int length = DST. getlength (); cstring strchn = _ T (""); for (I = 0; I <length; I ++) {If (unsigned char) DST. getat (I) & gt; = 128) {strchn + = DST. getat (I ++); strchn + = DST. getat (I) ;}return strchn;} cstring getenglish (const cstring & DST) {int I; int length = DST. getlength (); cstring streng = _ T (""); for (I = 0; I <length; I ++) {If (unsigned char) DST. getat (I) <128) {streng + = DST. getat (I) ;}return streng ;}

Appendix previous blog http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html about cstring API

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.