Standardized programming: use of ANSI and Unicode

xiaoxiao2021-03-06  62

Standardized programming: ANSI and Unicode use what is ANSI, what is Unicode? In fact, this is two different coding standards, and the characters in ANSI use 8bit, and the characters in Unicode use 16bit. 8bit ANSI encoding can only represent 256 characters, indicating that 26 English letters are more than enough, but it is not enough to represent Chinese characters, Korean, Japanese, etc., there is not enough non-Western characters, which is not enough. Unicode standard.

In software development, especially some of the string processing using the C language, ANSI and Unicode are distinguished, then how the ANSI type characters and Unicode type characters are defined, how to use? How does ANSI and Unicode conversion?

One. Definition part:

ANSI: CHAR STR [1024]; Available string processing functions: strcpy (), strcat (), strlen (), etc. Unicode: Wchar_t Str [1024]; Available String Processing Functions

two. Available functions:

ANSI: That is, CHAR, the available string processing function: strcat (), strcpy (), strlen () or the like a function of the STR header.

Unicode: WCHAR_T Available String Processing Functions: WCSCAT (), WCSCPY (), WCSLEN (), etc. Functions in WCS headers.

three. System Support

Windows 98: Only ANSI is supported.

Windows 2k: Supports both ANSI and Unicode.

Windows CE: only supports Unicode.

Description

1. I only support Unicode in COM.

2. Windows 2000 The entire OS system is Based on Unicode. To this, use ANSI to use ANSI to pay at Windows 2000, although this conversion is hidden, which is an occupied system resource (CPU, RAM).

3. Uneicode must be used in Windows 98, you need to manually encode switching.

three. How to differentiate:

In our software development, it is often necessary to support ANSI and support Unicode. It is impossible to re-changing the type of string while requiring type conversion, and using an operation function on a string. To this end, the standard C run library and Windows provide macro definitions.

The _unicode macro (underline) is provided in the C language, providing a Unicode macro (no underline) in Windows, as long as the _unicode macro and Unicode macro, the system will automatically switch to the Unicode version, otherwise, the system follows ANSI Compile and run in the way.

Only the macro does not implement automatic conversion, and he needs a series of character definition support.

1. TCHAR

If the Unicode macro TCHAR is defined as Wchar_T.

Typedef wchar_t tchar;

Otherwise, TCHAR is defined as char.

Typedef char tchar;

2. LPTSTR

If the Unicode macro LPTSTR is defined as LPWSTR.

Typedef lptstr lpwstr;

Otherwise, TCHAR is defined as char.

Typedef lptstr lpstr;

转载请注明原文地址:https://www.9cbs.com/read-83105.html

New Post(0)