ANSI and UNICODE programming considerations

Source: Internet
Author: User

Create a UNICODE Code project

In VC60, the ANSI encoding project is created by default (Note: The resource characters of the compiled exe are saved in UNICODE). The UNICODE encoding project is created using the following method:

1. Add UNICODE and _ UNICODE preprocessing options for the project.

Specific steps: open [project]-> [settings…] In the "pre-processing program definition" in the C/C ++ label dialog box, remove _ MBCS and add _ UNICODE and UNICODE. (Note: Separate them with commas ).

Before UNICODE and _ UNICODE are defined, all functions and types Use the ANSI version by default. After UNICODE and _ UNICODE are defined, all the MFC classes and Windows APIs have been changed to the wide-byte version.

2. Set the program entry point

Because the MFC application has a program entry point dedicated to Unicode, we need to set the entry point. Otherwise, a connection error occurs.

To set an entry point, open [project]-> [set…]. Dialog box, fill in wWinMainCRTStartup in the Category: Output Entry Point on the Link Page.

Several precautions for programmers

1. Use THCAR to replace char

2. Add _ T ("") to the string, for example, _ T ("hello ")

3. Replace strcpy with _ tcscpy

ANSI operation functions start with str, such as strcpy (), strcat (), strlen ();

Unicode operation functions start with the wcs, such as wcscpy, wcscpy (), and wcslen ();

ANSI/Unicode compatible operation functions start with _ tcs _ tcscpy (C Runtime Library );

ANSI/Unicode compatible operation functions start with lstr lstrcpy (Windows function );

Considering compatibility between ANSI and Unicode, we need to use a universal string operation function starting with _ tcs or starting with lstr.

In this way, you can not modify the code when compiling in the ANSI/UNICODE environment.

ANSI/UNICODE character conversion and other

1. in ANSI, if the character (string) generated by the program itself, such as the CString str = _ T ("hello") directly assigned in the code, no conversion is required. However, character (string) comes from other places, such as reading UNICODE-encoded files and receiving UNICODE data through communication. Conversion is required using the above two functions.

2. Similarly, in UNICODE, if the character (string) generated by the program itself, such as the CString str = _ T ("hello") directly assigned in the code, no conversion is required, however, the character (string) comes from other places. For example, if you want to read an ANSI-encoded file and send ANSI data received by communication, you need to convert the data using the above two functions.

3. Whether it is ANSI or UNICODE, the strings in the resource, such as the static character of the character dialog box in String table, are not converted. After compilation, unicodeis stored in .exe.

The Chinese characters in the dialog box must be simplified and simple. A. EXE running on both simplified Windows and traditional Windows can be displayed normally. Of course, the two must have a corresponding font (Library) and both must use the System font, you can also use "" in simplified Windows and "" in traditional Windows ". The Simplified Chinese font is the same as the traditional Chinese font. The simplified Chinese font is different from the traditional Chinese font.

4. In addition, the same string has different counts under ANSI and UNICODE. For example, "123 hello" is 7 characters in ANSI and 5 Characters in UNICODE. Pay special attention to the use of CString. GetLength.

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.