Reproduced in: http://www.cnblogs.com/lihao102/archive/2013/04/14/3020229.html
First, we will introduce a Windows character set. Windows supports two types of character sets, multi-byte and wide character (UNICODE, all are implemented using wide characters, but the API interfaces supported by multiple bytes are also retained. The principle of this implementation is to convert it to wide characters after receiving multiple characters, then upload the kernel object for processing.
It also needs to be noted that when Mircosoft converts com from 16 bits to 32 bits, it specifies that only the Unicode string is accepted by the string method.
The following describes two macros.
The header file of the Runtime Library defines the macro Unicode of the Unicode Environment
Windows header file defines the Unicode environment macro _ Unicode
The C Runtime Library provided by Microsoft is consistent with the ANSI standard C Runtime Library, while ANSI requires that the Runtime Library must support Unicode and ANSI (Multi-byte) strings and characters, therefore, the Windows Runtime Library also supports the multi-byte and wide character encoding operations.
The most typical multi-byte interfaces include strlen, strcat, and strcmp. The wide character interfaces include wcslen, wcscat, and wcscmp.
With the above foundation, we started to talk about theme.
Because the ARX environment must support multiple versions, character operations are very common in ARX, therefore, code that can be compiled in both a multi-byte environment and a wide character environment can be solved. You don't need to worry. The same problem exists during windows development. Therefore, Microsoft provides a solution, and the key is in the header file tchar. h.
In tchar. in the H file, the adaptive character encoding and operation interfaces are implemented based on whether the _ Unicode macro is defined. For example, in this file, the definition of char is like this, when the defined _ Unicode macro is typedef wchar_t tchar, and if no definition is made, typedef char tchar. Therefore, when defining a string or character, we only need to use tchar to replace char, this enables Adaptive Character definitions in the Multi-byte and wide character environments.
After definition, we are concerned about assigning values to variables correctly. In the Runtime Library, we provide a method. When an uppercase L is added before a String constant, the compiler is notified, this string is compiled as a unicode string, so in tchar. H also uses this to define the mechanism for notifying the compiler character encoding as the environment changes. When _ Unicode is defined, # DEFINE _ text (x) L # X. When no definition is made, # DEFINE _ text (x) X, in this way, you only need to add the _ text () operation to each constant string to automatically compile the environment. In tchar. in H, _ text is defined as a shorter and well-known _ T. Therefore, we only need to add the _ T () operation to each constant string to adapt to the compiling environment.
In addition, tchar. h, also for _ Unicode macro definition, implements a set of adaptive encoding Environment Character operation interface, so when ARX programming, If you need multi-version support, make sure to use tchar. h.
Note that the cstring in MFC is an environment-Adaptive Character Set. Therefore, we recommend that you use cstring instead of STD: String in programming.
Finally, we will post out a common and alternative method that requires attention:
1. Use the cstring type whenever possible
2. Use tchar for character types
3. Use tchar [] for string Arrays
4. Use tchar * for string pointers *
5. The constant string pointer uses const tchar *
6. Adding a String constant and a character constant to a macro _ t
Special:
1> cstring: Format (_ T ("..."),...) // Add _ t
2> use the basic type const tchar * (or tchar *) instead of the macros defined by multiple nesting such as lptstr;
3> space allocated with N-1 characters for the tchar * type pointer: tchar * pbuffer = new tchar [N * sizeof (tchar)];
Common string functions must be replaced by macro functions that support Unicode/ANSI:
ANSI string functions Unicode/ANSI macro functions
Strcpy _ tcscpy string copy
Strcat _ tcscat string connection
Strlen _ tcslen evaluate the string length
Strcmp _ tcscmp string comparison
Atof _ tcstod string to double
Atoi _ ttoi string to int
Note: In the table above, _ tcstod and atof are used differently and cannot be replaced directly.
Tchar and cstring convert each other
Cstring STR = _ T ("hello ");
Char s [50];
S = (lptstr) (lpctstr) STR;
STR = (lpctstr) (lpstr) S;
Enclose the character set corresponding to each ObjectARX version:
Objectarx2007 and above are Unicode (wide character) encoding.
The following are all ANSI (Multi-byte) encoded objectarx2007.