Differences between tchar, wchar_t, Char, and string processing functions)

Source: Internet
Author: User
I. ANSI and Unicode 2. ANSI and Unicode charactersThe ANSI character type is Char, pointing to the string pointer pstr (lpstr), pointing to a constant string pointer pcstr (lpcstr ); the corresponding windows-defined UNICODE character type is wchar (typedef wchar wchar_t), pointing to the Unicode string pointer pwstr, pointing to a constant Unicode string pointer pcwstr. ANSI "ANSI" Unicode l "Unicode" ANSI/Unicode T ("string") or _ text ("string ") 3. Operations on ANSI characters and Unicode stringsIn the double byte (DBCS) Character Set, each character of a string can contain one or two bytes. If you only call the strlen () function, you cannot know the number of characters in the string. It can only tell you the number of bytes before reaching the end of 0. In standard C, strcpy, strchr, and strcat can only be used for ANSI strings and cannot correctly process Unicode strings. Therefore, a set of complementary functions are provided, which are equivalent in functionality but used for Unicode codes. Let's take a look at string. how to deal with char * And wchar_t * in the H string header file: char * strcat (char *, const char *); wchar_t * wcschr (wchat_t *, const wchar_t *) similar to strchr/wcschr, strcmp/wcscmp, strlen/wcslen etc. ANSI operation functions start with str strcpy Unicode operation functions start with WCS wcscpy MBCS operation functions start with _ MBS _ mbscpy ANSI/Unicode operation functions start with _ TCS _ tcscpy (C Runtime Library) ANSI/Unicode operation functions start with lstr lstrcpy (Windows function) Ii. ANSI/Unicode common characters/string typesTchar/lptstr/lpctstr Neutral ANSI/Unicode types1. general character tchartcharifdef Unicode it is wchar_t (wchar) for Unicode platforms; else it is Char For ANSI and DBCS platforms.2. general character string pointer lptstrlptstrifdef Unicode it is lpwstr (* wchar_t) for Unicode platforms; else it is lpstr (* char) For ANSI and DBCS platforms.3. general purpose constant string pointer lpctstrlpctstrifdef Unicode it is lpcwstr (* const wchar_t) for Unicode platforms; else it is lpcstr (* const char) For ANSI and DBCS platforms. Typedef lpwstr LP; # DEFINE _ text (quote) L # quote // r_winnt <1> _ Unicode macro is used for C Runtime header files, and Unicode macro is used for Windows header files, when compiling code modules, these two macros must be defined at the same time. <2> If _ Unicode is defined, to generate a unicode string, add an L macro before the string to tell the compiler that the string should be compiled as a unicode string. However, another problem is that compilation fails if _ Unicode is not defined. To solve this problem, we must use the _ text macro, which is also defined in tchar. h. After the macro is used, no compilation error occurs no matter whether the source file has a definition _ Unicode. <3> Unicode and ANSI String Conversion: the WINDOWS function multibytetowidechar is used to convert a multi-byte string to a wide string. The function widechartomultibyte converts a wide string to an equivalent multi-byte string. Some people love to use standard ANSI functions such as strcpy, and some love the _ txxxx function. It is necessary to clarify the ins and outs. To understand these functions, you must write several character types. Not to mention Char. Let's talk about wchar_t first. Wchar_t is the data type of Unicode characters. It is actually defined in <string. h>: typedef unsigned short wchar_t; you cannot use ansi c string functions such as strcpy to process wchar_t strings. You must use functions prefixed with the WCS, such as wcscpy. To enable the compiler to recognize Unicode strings, you must add an "L" to the front, for example, wchar_t * sztest = l "this is a unicode string. "wchar_t is the data type of Unicode characters. It is actually defined in <string. h> in: typedef unsigned short wchar_t; let's take a look at tchar. If you want to compile source code for both ANSI and Unicode, You need to include tchar. h. Tchar is a macro defined in it. It is defined as Char or wchar_t based on whether _ Unicode macro is defined. If you use tchar, you should not use the ANSI strxxx function or the Unicode wcsxxx function. Instead, you must use the _ tcsxxx function defined in tchar. h. In addition, to solve the problem with "L" mentioned earlier, tchar. h defines a macro: "_ text ". Take the strcpy function as an example :. if you want to use an ANSI string, use this method: Char szstring [100]; strcpy (szstring, "test ");. if you want to use Unicode strings, use this set: wchar_t szstring [100]; wcscpyszstring, l "test ");. if you want to compile ANSI or Unicode string code by defining _ Unicode macro: tchar szstring [100]; _ tcscpy (szstring, _ text ("test"); 2, ANSI and unicodeunicode are called wide character strings, and all Unicode strings are used in COM. Converts ANSI to Unicode (1) using the L macro. For example, clsidfromprogid (L "mapi. folder ", & CLSID); (2) Implement conversion through the multibytetowidechar function, for example, char * szprogid =" mapi. folder "; wchar szwideprogid [128]; CLSID; long llen = substring (cp_acp, 0, szprogid, strlen (szprogid), szwideprogid, sizeof (szwideprogid )); szwideprogid [llen] = '(3) is implemented through the a2w macro, such as uses_conversion; clsidfromprogid (a2w (szprogid), & CLSID); Unicode is converted to ANSI (1) Using widechartomultibyte, for example: // assume that you already have a unicode string wszsomestring... char szansistring [max_path]; widechartomultibyte (cp_acp, signature, wszsomestring,-1, szansistring, sizeof (szansistring), null, null); (2) use the w2a macro, for example: uses_conversion; ptemp = w2a (wszsomestring );

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.