[C++]各种类型转换整理,不定时补充

各种数据类型转换整理,不定时补充,也欢迎大家补充,如有错误,还请批评指正,谢谢~

以下是代码内容:

// string转CString - start-
string a="abc";
CString str=CString(a.c_str());
//或 
str.Format("%s", a.c_str());
// string转CString - end-

// int转CString - start -
int a=1;
CString Cstr(_T(""));
Cstr.Format(_T("%d"),a);
// int转CString - end -

// char转CString - start -
char szPath[];
CString Cstr(_T(""));
Cstr.Format(_T("%s"), szPath);
// char转CString - end -

// CString转string - start -
CString C_str=_T("abc");
string str((LPCSTR)CStringA(C_str));

//或 
string str=CStringA(C_str);

//或通过char*中转(1),如下:
CString m_str(_T("qwerg"));
char *chr=new char[m_str.GetLength()+1];
WideCharToMultiByte(CP_ACP,0,m_str.GetBuffer(),-1,chr,m_str.GetLength()+1,NULL,NULL);
string str=chr;

//或者(2)
CString C_str = _T("ooqoqoq");
char *p = new char[C_str.GetLength()+1];
wcstombs(p,C_str,C_str.GetLength()+1);
string str = p;
// CString转string - end -

// CString转const char* - start -
CString a(_T(""));
CStringA b(a);
const char *c=new char(50);
c=b.GetString();

//或者:
CString FilePath=_T("");
int len=WideCharToMultiByte( CP_ACP,0,FilePath.AllocSysString(),FilePath.GetLength(),NULL,0,NULL,NULL);
char * pAscii =new char[len];
len=WideCharToMultiByte(CP_ACP,0,FilePath.AllocSysString(),FilePath.GetLength(),pAscii,len+1,NULL,NULL);
pAscii[len]=0;    
const char* path=(const char*)pAscii;
// CString转const char* - end -

// CString转char* - start -
CString strPath = _T("啊啊啊");
int nLength = strPath.GetLength();
int nBytes = WideCharToMultiByte(CP_ACP,0,strPath,nLength,NULL,0,NULL,NULL);
char* VoicePath = new char[ nBytes + 1];
memset(VoicePath,0,nLength + 1);
WideCharToMultiByte(CP_OEMCP, 0, strPath, nLength, VoicePath, nBytes, NULL, NULL);
VoicePath[nBytes] = '\0';
// CString转char* - end -

// CString转int - start -
CString str=_T("123");
int b=_ttoi(str);
//或者转成string,再转int
// CString转int - end -

// char转string -start -
string s(char *);
// char转string -end -

// char[]转LPWSTR
//直接强制转换

// string转char* - start -
//把string转换为char* 有3种方法:
//1.data
string str="abc";
char *p=str.data();
//2.c_str
string str="gdfd";
char *p=str.c_str();
//3. copy
string str="hello";
char p[40];
str.copy(p,5,0); //这里5,代表复制几个字符,0代表复制的位置
*(p+5)='\0'; //要手动加上结束符
// string转char* - end -

// string转int - start -
string num;
int a=atoi(num.c_str());
// string转int - end -

// int转string - start - 
stringstream ss;
int n = 123;
string str;
ss<<n;
ss>>str;
// int转string - end - 

// CString转_bstr_t,直接强制转换 - start -
CString  str1;
( _bstr_t)str1;
// CString转_bstr_t,直接强制转换 - start -

// CString to char* - start -
CString strBuf="awefasdf";
char *pBuff=strBuf.GetBuffer(0);
// CString to char* - end -

// char[]转CString - start -
char chInfo[1024];
memset(chInfo, 0, 1024);
strcpy_s(chInfo, "Hello World!")
CString str = chInfo;

//或者
CString str(chInfo);
// char[]转CString - end -

// CString to LPCSTR - start -
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
// CString to LPCSTR - end -

// char[]转CString - start -
char str1[]= {"123456"};
LPSTR lpstr = str1; 
CString str2 = CString(lpstr );
//或者
CString str2(lpstr );
// char[]转CString - end -

// CString to LPCSTR - start -
CString str = _T("test");
LPCTSTR lpctstr = str.GetBuffer();
//这里使用这个lpctstr记得ReleaseBuffer()
str.ReleaseBuffer(); 
// CString to LPCSTR - end -


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页