Programming has been a headache recently. This type of conversion can be done by knowing that it can be switched, but it is always hard to remember. I will keep searching through the Internet and summarize it here. For future convenience, of course, some methods may not be the latest or the simplest, but they should be much more convenient to use as you know:
1 "string to wstring
wstring s2ws(const string s)
{
_bstr_t t = s.c_str();
wchar_t* pwchar = (wchar_t*)t;
wstring result = pwchar;
ret
The cstring class is more powerful than the string class of STL. It is always powerful when new users use cstring.
However, due to lack of understanding about its internal mechanism, when a newbie converts a cstring to a C character array
Many problems may occur. Because the cstring operator is already overloaded, The
Convert cstring to char *
Cstring CSTR;
Char * P = (lpstr) (lpctstr) CSTR;
String to cstringCstring. Format ("% s", String. c_str ());
Convert Char to cstringCstring. Format ("% s", char *);
Char to stringString S (char *);
String to char *Char * P = string. c_str ();
Cstring to stringString S (cstring. g
The cstring class is more powerful than the string class of STL. It is always powerful when new users use cstring.
However, due to lack of understanding about its internal mechanism, when a newbie converts a cstring to a C character array
Many problems may occur. Because the cstring operator is already overloaded, The
a project by default and is compiled by Unicode ).
1. Added UNICODE and _ UNICODE pre-processing definitions.
Location: Project Settings-> C/C ++-> Preprocessor definitions
After these two definitions are added, some built-in types of MFC, such as TCHAR and CString, will be converted to support the wide character type (wchar_t)
2. Use the wide character type, such:
Char-> TCHAR, char *-> LPTSTR, const char *-> LPCTSTR
3. Use _ T () macro for string c
a project by default and is compiled by Unicode ).
1. Added Unicode and _ Unicode pre-processing definitions.
Location: Project Settings-> C/C ++-> Preprocessor Definitions
After these two definitions are added, some built-in types of MFC, such as tchar and cstring, will be converted to support the wide character type (wchar_t)
2. Use the wide character type, such:
Char-> tchar, char *-> lptstr, const char *-> lpctstr
3. Use _ T () macro for string c
Relationship and difference between cstring, BSTR, and lpctstr
Cstring is a dynamic tchar array, and BSTR is a string in a proprietary format (it needs to be manipulated using the functions provided by the system. The lpctstr is just a constant tchar pointer.
Cstring is a completely independent class, dynamic tchar array, and encapsulates operators such as + and
String to cstring
Cstring. Format ("% s", String. c_str ());
Convert Char to cstringCstring. Format ("% s", char *);
Char to stringString S (char *);
String to char *Char * P = string. c_str ();
Cstring to stringString S (cstring. getbuffer ());
1, string-> cstringCstring. Format ("% s", String. c_str ())
Cstring is a dynamic tchar array, and BSTR is a string in a proprietary format (it needs to be manipulated using the functions provided by the system. The lpctstr is just a constant tchar pointer.
Cstring is a completely independent class, dynamic tchar array, and encapsulates operators such as + and string operation methods.Typedef olechar far * BSTR;Typedef const char * lpctstr;
Representation of various
This article summarizes CString left (), Mid (), right () and several CString string lookup related functions to use, first need to find, CString has the following several lookup functions, respectively, find (), Reversefind (), Findoneof ().
One, CString find (), findoneof (), Reversefind ()
All three functions retu
Type understanding
Lpctstr type:
L indicates the long pointer, which is left behind by Windows 3.1 and other 16-bit operating systems. In Win32 and other 32-bit operating systems, long pointers, near pointers, and far modifiers are designed for compatibility. No practical significance.
P indicates that this is a pointer.
C Indicates a constant.
T indicates that there is a _ t macro In the Win32 environment.
STR indicates that this variable is a string description. This macro is used to ind
Recently, the conversion of cstring, tchar *, and char * is used.
Find a detailed article. Reference it for your convenience.
Char is type. tchar is also! However, he can determine whether it is Char or w_char by defining a unicode macro;
Tchar is a string type that allows you to build in MBCS and unnicodeProgramYou can use the sameCode, You do not need to use tedious macro definitions to include your code, and char represents ASCII characters
I encountered an interesting problem in a project that I used wtl before. We first made a prototype version, which is a separate exe. Later, it was divided into different modules. In addition to the interface, other modules were implemented using DLL. The wtl: cstring class is widely used in the unbounded DLL module, because this implementation is faster and more comprehensive than the cstring Implementatio
The working principle of cstring (zz) has read many programs, including some code I have written, A large part of bugs are found in the incorrect usage of cstring In the MFC class. the main cause of this error is that you do not know much about the cstring implementation mechanism.
Cstring is a packaging of the string
CString in today's software design industry is still a little famous, said it is the most used in MFC class is not at all, however, when using the SDK programming windows, it is not possible to use the CString class, only the SDK runtime library, such as strlen,strcpy, Strstr and so on, this article is discussed in the Non-MFC program to use the CString class met
Source: csdn Author: Brant
The cstring class is a frequently used class, so it is necessary to analyze its memory management mode. The evolution of memory management is as follows:Vc5 simply uses the new Delete method.Because the memory size needs to be adjusted frequently for string operations, the C ++ operator new and delete have no function corresponding to realloc. The result is that an additional copy operation is required for every memory cha
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.