Conversion between char*, string and CString in MFC (other data types)

In the MFC program, you can use the Format method to easily convert numeric types such as int, float, and double to CString strings. The following is a description of the formats supported by CString's Format:

%c single character
%d decimal integer (int)
%ld decimal integer (long)
%f decimal floating point (float)
%lf decimal floating point (double)
%o octal number
%s string
%u unsigned decimal number
%x hexadecimal number


1. Convert the CString class to char* (LPSTR) type

The first method is to use coercion. 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); 
Method 3, use CString::GetBuffer. For example: 
CString s(_T("This is a test ")); 
LPTSTR p = s.GetBuffer(); 
// add code using p here 
if(p != NULL) *p = _T('\0' ); 
s.ReleaseBuffer(); 
// Release in time after use so that other CString member functions can be used

CString str = "ABCDEF"; 
char *pBuf = str,GetBuffer( 0 ); 
str.ReleaseBuffer();

Two, string to char*

String is one of the C++ standard library, which encapsulates the operation of strings and
converts strings to char*. There are 3 methods:
1. data(), returns a string array without "\0" 
such as:
string str="abc";
char *p=str.data();
2.c_str returns a string array with "\0" 
such as: string str ="gdfd";
    char *p=str.c_str();
3 copy
such as
string str="hello";
char p[40];
str.copy(p,5,0); //here 5, represents the number of copies characters, 0 represents the copy position
*(p+5)='\0'; //To manually add the terminator
cout < < p;

Three, string string converted to other data types

temp="123456";
1) Short (int)
i = atoi(temp);
2) Long (long)
l = atol(temp);
3) Floating point (double)
d = atof(temp);
string s; d= atof(s.c_str());
4) BSTR variable
BSTR bstrValue = ::SysAllocString(L"Programmer");
...///Complete the use of bstrValue
SysFreeString(bstrValue);
5) CComBSTR variable
CComBSTR type variable can be directly assigned
CComBSTR bstrVar1("test");
CComBSTR bstrVar2(temp);
6) _bstr_t variable
_bstr_t type variable can be directly assigned
_bstr_t bstrVar1("test");
_bstr_t bstrVar2(temp);

4. Convert Char* to string

To convert a char to a string, use  string s(char *); 

五、string 转CString  
CString.format("%s",  string.c_str()); 

6. char to CString  
CString.format("%s", char*); 

七、     CString -> string 

string s(CString.GetBuffer());  
Be sure to ReleaseBuffer() after GetBuffer(), otherwise the space occupied by the buffer will not be released.

Eight, CString conversion int

To convert characters to integers, you can use atoi, _atoi64 or atol, _ttoi, _atoi.

CString str("1234");
int i= _ttoi(str);

code that can be used in both ANSI and UNICODE

CString cs;
char szBuf[128];
int iVar;
strcpy( szBuf, cs.GetBuffer( cs.GetLength( ) ) );
cs.ReleaseBuffer( );
iVar = atoi( szBuf );

int to CString


CString str;
int number=15;

//str="15"
str.Format(_T("%d"),number);

//str="15"(There are two spaces in front; 4 means it will occupy 4 bits , if the number exceeds 4 digits, it will output all digits without truncation)
str.Format(_T("%4d"),number);

//str="0015"(.4 means it will occupy 4 digits, if the number exceeds 4 bits will output all numbers, no truncation)

str.Format(_T("%.4d"),number);


The method of converting long to CString is similar to the above, just change %d to %ld.

double to CString

CString str;
double num=1.46;

//str="1.46"
str.Format(_T("%lf"),num);

//str="1.5"(.1 means leave 1 digit after the decimal point, more than 1 bit is rounded up)
str.Format(_T("%.1lf"),num);

//str="1.4600"
str.Format(_T("%.4f"),num);

//str=" 1.4600 "(There is a space in front)
str.Format(_T("%7.4f"),num);

The method of converting float to CString is similar to the above, just change lf% to f%.


Convert CString to double

CString ss="123.21";

double dd=atof(ss);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951793&siteId=291194637