Conversion of cstring and char * In MFC

Source: Internet
Author: User

A cstring variable CSTR
A char * variable ch
How to convert CSTR to ch?

1. Pass the const char * (lpctstr) pointer to the unallocated memory.
Cstring CSTR (asdd );
Const char * Ch = (lpctstr) CSTR;
Ch points to the same address as CSTR. However, since the use of const ensures that CH will not be modified, it is safe.
2. Pass the pointer to the unallocated memory.
Cstring CSTR = "asddsd ";
Char * Ch = CSTR. getbuffer (CSTR. getlength () + 1 );
CSTR. releasebuffer ();
// Modify the value that CH points to is equal to the value in CSTR.
// PS: After the CH is used up, the delete ch is not used, because this will destroy the internal space of CSTR and cause program crash.
3. 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. Method 3: Assign the cstring value to the allocated memory char [] array.
Cstring cstr2 = "asddsd ";
Int strlengh2 = cstr2.getlength () + 1;
Char charray [100];
Memset (charray, 0, sizeof (bool) * 100); // clears the junk content of the array.

Strncpy (charray, cstr2, strleng22 );

 

1. Convert string to cstring. format ("% s", String. c_str (); 2. Convert char * To cstring. format ("% s", char *); 3. Convert char * to string S (char *); 4. Convert string to char * P = string. c_str (); 5, cstring to string S (cstring. getbuffer (cstring. getlength (); 6. Convert cstring to char * charpoint = strtest. getbuffer (strtest. getlength (); it is not recommended to use (lpctstr) for forced type conversion, so that an error occurs when the strtest size changes. 7. Convert cstring to Char [100] char a [100]; cstring STR ("aaaaaa"); strncpy (A, (lpctstr) STR, sizeof ());

Attached: cstring-related member functions: http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html

Microsoft's summary: http://msdn.microsoft.com/zh-cn/library/vstudio/ms235631.aspx

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.