|
A CString variable cstr
A char * variable ch
How to convert cstr to ch?
1. A const char * (LPCTSTR) pointer passed to unallocated memory.
CString cstr (asdd);
Const char * ch = (LPCTSTR) cstr;
The address pointed to by ch is the same as cstr. But because const is used to guarantee that ch will not be modified, it is safe.
2.A pointer passed to unallocated memory.
CString cstr = "ASDDSD";
Char * ch = cstr.GetBuffer (cstr.GetLength () + 1);
Cstr.ReleaseBuffer ();
// Modifying the value pointed to by ch is equal to modifying the value in cstr.
// PS: After using ch, do not delete ch, because this will destroy the internal space of cstr and easily cause the program to crash.
3. The second usage. Assign the CString value to the char * of the allocated memory.
CString cstr1 = "ASDDSD";
Int strLength = cstr1.GetLength () + 1;
Char * pValue = new char [strLength];
Strncpy (pValue, cstr1, strLength);
4. The third usage. Assign the CString value to the char [] array of allocated memory.
CString cstr2 = "ASDDSD";
Int strLength2 = cstr2.GetLength () + 1;
Char chArray [100];
Memset (chArray, 0, sizeof (bool) * 100); // Empty the garbage content of the array.
Strncpy (chArray, cstr2, strLength2);
Zh
1.String to CString
CString.format ("% s", string.c_str ());
Char * to CString
CString.format ("% s", char *);
3.char * to string
string s (char *);
4.String to char *
char * p = string.c_str ();
5.CString to string
string s (CString.GetBuffer (CString.GetLength ()));
6.CString to char *
charpoint = strtest.GetBuffer (strtest.GetLength ());
It is not recommended to use (LPCTSTR) for forced type conversion, so an error will occur when the size of strtest changes.
7. CString to char [100]
char a [100];
CString str ("aaaaaa");
strncpy (a, (LPCTSTR) str, sizeof (a));
Attachment: Cstring related member functions: http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html
Microsoft's summary of this: http://msdn.microsoft.com/zh-cn/library/vstudio/ms235631.aspx
————————————————
Copyright statement: This article is an original article by the CSDN blogger "DayThinking", which follows the CC 4.0 BY-SA copyright agreement. Please reprint the original source link and this statement.
Original link: https://blog.csdn.net/sszgg2006/java/article/details/8667945 |
|