Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to distinguish between c++wchar_t, char and WCHAR character types

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to distinguish between c++wchar_t, char and WCHAR character types, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1. Distinguish between wchar_t,char,WCHAR

ANSI: char. String handling functions are available: strcat (), strcpy (), strlen (), and other functions that start with str.

UNICODE:wchar_t is the data type of Unicode characters, which is actually defined in:

Typedef unsigned short wchar_t

In addition, there is a definition in the header file: typedef wchar_t WCHAR;, so WCHAR is actually wchar_t

Wchar_t can use string processing functions: wcscat (), wcscpy (), wcslen () and other functions that start with wcs. In order for the compiler to recognize Unicode strings, you must precede them with an "L", such as: wchar_t * szTest=L "This is a Unicode string."

2 、 TCHAR

The _ UNICODE macro (with underscore) is provided in C language, and the UNICODE macro (no underscore) is provided in Windows. As long as you set the _ Unicode macro and Unicode macro, the system will automatically switch to the UNICODE version, otherwise, the system will compile and run in the way of ANSI. Defining a macro alone does not achieve automatic conversion, it also needs a series of character definition support.

1. TCHAR

If a UNICODE macro is defined, TCHAR is defined as wchar_t.

Typedef wchar_t TCHAR

Otherwise TCHAR is defined as char typedef char TCHAR

2. LPTSTR

If a UNICODE macro is defined, LPTSTR is defined as LPWSTR.

Typedef LPTSTR LPWSTR

Otherwise TCHAR is defined as char typedef LPTSTR LPSTR

Note: when using string constants, you need to use _ TEXT ("MyStr") or _ T ("") to support automatic conversion of the system.

3 、 BSTR

BSTR is a string with a length prefix, which is mainly managed by the operating system, so use api. Mainly used to deal with VB (string in VB refers to it) there are many API functions to operate it. Such as SysAllocString,SysFreeString and so on.

The classes that encapsulate it in vc such as _ bstr_t and CComBSTR in ATL.

A BSTR consists of a header and a string. The header contains the length information of the string, and the string can contain embedded null values.

BSTR is passed in the form of a pointer. (a pointer is a variable that contains the memory address of another variable, not data.) BSTR is Unicode, that is, each character requires two bytes. BSTR usually ends with a two-byte null character. Wstr is a wide character, representing a character in double bytes bstr is to be compatible with the original basic character, its first 4 bytes are its length, ending with'\ 0'.

4. Type definition of further strings and their pointers

Because the function list in the Win32 API document uses the common name of the function (for example, "SetWindowText"), all strings are defined in TCHAR. (except for the API introduced in XP that applies only to Unicode). Here are some commonly used typedefs, which you can see in msdn.

TypeMeaning in MBCS buildsMeaning in Unicode buildsWCHARwchar_twchar_tLPSTRchar*char*LPCSTRconst char*const char*LPWSTRwchar_t*wchar_t*LPCWSTRwchar_t*wchar_t*TCHARTCHAR charwchar_tLPTSTRTCHAR*TCHAR*LPCTSTRconst TCHAR*const TCHAR*

5. Mutual conversion

(1) convert char* to CString

If you convert char* to CString, in addition to direct assignment, you can also use CString::Format. For example:

Char chArray [] = "This is a test"

Char * p = "This is a test"

Or

LPSTR p = "This is a test"

Or in the defined Unicode user program

TCHAR * p = _ T ("This is a test")

Or

LPTSTR p = _ T ("This is a test")

CString theString = chArray

TheString.Format (_ T ("% s"), chArray)

TheString = p

(2) convert CString to char*

When you convert a CString class to a char* (LPSTR) type, you often use the following three methods:

Method one is to use a cast.

For example:

CString theString ("This is a test")

LPTSTR lpsz = (LPTSTR) (LPCTSTR) theString

Method 2, use strcpy.

For example:

CString theString ("This is a test")

LPTSTR lpsz = new TCHAR [theString.GetLength () + 1]

_ tcscpy (lpsz, theString)

It is important to note that the second argument to strcpy (or the _ tcscpy of the removable Unicode/MBCS) is const wchar_t* (Unicode) or const char* (ANSI), which will be automatically converted by the system compiler.

Method 3, use CString::GetBuffer.

For example:

CString s (_ T ("This is a test"))

LPTSTR p = s.GetBuffer ()

/ / add the code that uses p here

If (p! = NULL) * p = _ T ('\ 0')

S.ReleaseBuffer ()

/ / release immediately after use, so that other CString member functions can be used.

(3) convert BSTR to char*

Method one, use ConvertBSTRToString.

For example:

# include

# pragma comment (lib, "comsupp.lib")

Int _ tmain (int argc, _ TCHAR* argv []) {

BSTR bstrText =:: SysAllocString (L "Test")

Char* lpszText2 = _ com_util::ConvertBSTRToString (bstrText)

SysFreeString (bstrText); / / release after use

Delete [] lpszText2

Return 0

}

Method two, overload using the assignment operator of _ bstr_t.

For example:

_ bstr_t b = bstrText

Char* lpszText2 = b

(4) convert char* to BSTR

Method one, use API functions such as SysAllocString.

For example:

BSTR bstrText =:: SysAllocString (L "Test")

BSTR bstrText =: SysAllocStringLen (L "Test", 4)

BSTR bstrText =:: SysAllocStringByteLen ("Test", 4)

Method two, use COleVariant or _ variant_t.

For example:

/ / COleVariant strVar ("This is a test")

_ variant_t strVar ("This is a test")

BSTR bstrText = strVar.bstrVal

Method three, use _ bstr_t, which is the easiest method.

For example:

BSTR bstrText = _ bstr_t ("This is a test")

Method 4, use CComBSTR.

For example:

BSTR bstrText = CComBSTR ("This is a test")

Or

CComBSTR bstr ("This is a test")

BSTR bstrText = bstr.m_str

Method five, use ConvertStringToBSTR.

For example:

Char* lpszText = "Test"

BSTR bstrText = _ com_util::ConvertStringToBSTR (lpszText)

(5) convert CString to BSTR

This is usually done by using CStringT::AllocSysString.

For example:

CString str ("This is a test")

BSTR bstrText = str.AllocSysString ()

...

SysFreeString (bstrText); / / release after use

(6) convert BSTR to CString

Generally speaking, it can be done in the following ways:

BSTR bstrText =:: SysAllocString (L "Test")

CStringA str

Str.Empty ()

Str = bstrText

Or

CStringA str (bstrText)

(7) conversion between ANSI, Unicode and wide characters

Method one, use MultiByteToWideChar to convert ANSI characters into Unicode characters, and use WideCharToMultiByte to convert Unicode characters into ANSI characters.

Second, use "_ T" to convert ANSI into a "normal" type string, use "L" to convert ANSI into Unicode, and in a hosted C++ environment, you can also use S to convert ANSI strings into String* objects. For example:

TCHAR tstr [] = _ T ("this is a test")

Wchar_t wszStr [] = L "This is a test"

String* str = S "This is a test"

Method 3. Use ATL 7. 0 to convert macros and classes. ATL7.0 perfects and adds a number of string conversion macros and provides corresponding classes based on the original 3.0. it has the unified form shown in figure 3:

The first C stands for "class" so that ATL 3.0 macros can be distinguished, the second C means constant, 2 means "to", and EX means to open up a buffer of a certain size. SourceType and DestinationType can be A, T, W, and OLE, meaning ANSI, Unicode, "normal" type, and OLE string, respectively. For example, CA2CT converts ANSI to a normal type of string constant.

Here are some sample code:

LPTSTR tstr= CA2TEX ("this is a test"); LPCTSTR tcstr= CA2CT ("this is a test"); wchar_t wszStr [] = L "This is a test"; char* chstr = CW2A (wszStr); this is the answer to the question on how to distinguish between c++wchar_t and char and WCHAR character types. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report