MFC CString Turn char *

Source: Internet
Author: User

interface when you encounter the need to accept input characters from the interface edit box (user name, password, etc.), and then to char * type to the program processing, record the method found, the main reference 5664733

    • Encoding method

The instructions for encoding are as follows, and after reading it, you understand why you need to convert.

Development is done under VS2015, the default character set encoding is Unicode, but in projects such as VC6.0, the default character set form is the multibyte character set (Mbcs:multi-byte Character set). This results in a very simple and useful variety of character operations and functions in the VC6.0 that will report a wide variety of errors when running in the VS2005 environment, summarizing several ways to convert between CString and char * under the Unicode character set in the VS environment. This is actually the Unicode character set and the MBCS character set conversion.

Let's start by explaining the size of the memory they occupy. CString belongs to the so-called wide character set, which accounts for two bytes of one character; the char type is a narrow character set and one char character takes one byte, so the conversion between them involves the conversion of the byte size. On the other hand, in MFC ctring, Unicode encoding is used by default, and Char is ANSI-encoded, and the storage size of a single character in two encodings is different.

1. CString converted to char under Unicode *

Method 1: Use the function T2A, W2A

CString CStr = _t ("test")// Declaration identification uses_conversion; // both the functions T2A and W2A support characters in ATL and MFC Char * pfilename = T2A (cstr);    //  // can also achieve conversion  <afxpriv.h>

This method has been validated and converted successfully in the VS2015 Unicode encoding environment.

Method 2: Use Api:widechartomultibyte for conversion

1CString str = _t ("Test");23 //Note: The following values of N and Len differ in size, and n is calculated by character, and Len is calculated as a byte4 intn =Str. GetLength ();5 6 //gets the size of a wide-byte character, measured in bytes7  intLen = WideCharToMultiByte (CP_ACP,0, Str,str. GetLength (), NULL,0, null,null);8  9 //request space for a multibyte-character array with an array size of wide-byte bytes, measured in bytesTen Char* Pfilename =New Char[len+1];//in bytes One  A //wide-byte encoding converted to multibyte encoding -WideCharToMultiByte (CP_ACP,0, Str,str. GetLength (), pfilename,len,null,null); -pfilename[len+1] ='/0';//multibyte character ends with '/0 '

This method has not been validated.

2, Unicode under char * converted to CString

Method 1: Use Api:multibytetowidechar for conversion

1 Char* Pfilename ="Test";2 3 //computes the char * array size, in bytes, of two bytes per kanji4 intCharlen =strlen (pfilename);5 6 //calculates the size of a multibyte character, calculated by character. 7 intLen = MultiByteToWideChar (CP_ACP,0, Pfilename,charlen,null,0);8 9 //request space for a wide-byte character array with an array size of multibyte character size computed in bytesTenTCHAR *buf =NewTchar[len +1]; One  A //Multi-byte encoding converted to wide-byte encoding -MultiByteToWideChar (CP_ACP,0, Pfilename,charlen,buf,len); -  theBuf[len] ='/0';//add end of string, note not len+1 -  - //convert the TCHAR array to CString - CString Pwidechar; + pwidechar.append (BUF); -  + //Delete buffer A Delete[]buf;

Method Two: Use function a2t, a2w

1 Char " Test " ; 2 3 uses_conversion; 4 CString s = a2t (pfilename); 5 // CString s = a2w (pfilename);

Method Three: Use the _t macro to convert a string to a wide character

1 // The writing code uses text ("") or _t (""), and the text is common in both Unicode and non-Unicode programs 2 AfxMessageBox (_t ("test string"));   3 4 Note: Direct conversion is possible in an MBCS-based project, but it is not feasible to convert directly in a Unicode-based project, CString will save the data in Unicode form, forcing the type conversion to return only the first character.

MFC CString Turn char *

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.