Getting Started with Unicode programming

Source: Internet
Author: User
Tags character set

Brief introduction

If you are writing programs that target non-English-speaking users, such as China, Japan, Eastern Europe, and the Middle East, then you must be familiar with the UNICODE character set. Especially if you are writing a program for users in these countries and regions with Visual C++/MFC, if you want your application to have a wider audience, you must consider code UNICODE compatibility, which means it runs in both ASCII mode and Unicode mode. 。 This article will introduce some basic programming knowledge of UNICODE to clarify the vague understanding that many people (including myself) have on this issue. For anyone using Visual C + + and/or MFC programming, this article is certainly worth reading.

What exactly is Unicode?

UNICODE is one of the more popular solutions currently used to address the 256-character limit on ASCII code. As you know, the ASCII character set is only 256 characters, represented by a number between 0-255. Includes uppercase and lowercase letters, numbers, and a few special characters, such as punctuation marks, currency symbols, and so on. For most Latin languages, these characters are sufficient. However, many Asian and Oriental languages use characters that are far more than 256 characters long. Some more than thousands. To break the limit of ASCII characters, people try to write computer programs in a language that is more than 256 characters in a simple way. So UNICODE emerged. UNICODE maps numeric code to a character set in multiple languages in a larger range by representing one character in two bytes.

Solutions for Visual C + +

As a software developer, how to use UNICODE skillfully and efficiently? If you are writing programs in Visual C + +, UNICODE compatibility means that your program is internationalized, that is, whether your application is targeted to the local or international market. Once you have made your decision, you need to implement specific details in your code. Fortunately, Visual C + + provides a number of built-in features to support UNICODE, and you can take advantage of these features provided by Visual C + + when you create a project. Before generating application framework code, AppWizard allows developers to decide whether to support UNICODE. The Win32 SDK contains data types that follow the Unicode encoding rules, and MFC provides a way to convert generic text to Unicode data types in macro form. Developers can easily write applications that support UNICODE simply by changing the code-writing habits a little bit.

String

C programmers generally use the CHAR keyword as follows to declare an array of strings:

char str [100];

Declare the prototype of the function as follows:

void strcpy( char *out, char *in );

To change the above declaration to support Double-byte UNICODE character sets, you can use the following method:

wchar_t str[100];

Or

void wcscpy( wchar_t *out, wchar_t *in );

In addition, Microsoft provides a preprocessing instruction to achieve UNICODE. Whenever you create a new project with Visual C + +, as long as you determine whether another character set is supported, AppWizard inserts a preprocessing directive in the header file. These instructions tell the compiler what character set the program wants to support. In this way, when using the common data type provided by VC + +, the compiler replaces the common data type with the required supported character set with the appropriate data type. This makes it easy to recompile code into programs that support other character sets.

To activate the UNICODE standard in Visual C + + 6.0, you can do this: After opening the project file, select Project | From the main menu. Settings Open the Project Settings dialog box => and then select the "C + +" tab => add UNICODE or _UNICODE preprocessing macro directives in the Preprocessor Definitions edit box. As shown in Figure one:

Figure One Project Settings dialog box

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.