1. string和char*
string转char* :
把string转换为char
*
有3中方法:
如:
1.data
如:
string str="abc";
char* p = (char*)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);
*(p+5)='\0';
char*转string
string转换为char*,可以使用string提供的函数c_str() ,或是函数data(),data除了返回字符串内容外,
不附加结束符'\0',而c_str()返回一个以‘\0’结尾的字符数组.
2.CString和char*
cstring转char*
1. 宽字节时:
CString str1 = "123";
char *p = (LPSTR)(LPCSTR) str1; 强转
还可以:
CString str1 = "123";
char *t1 = str1.GetBuffer(str1.GetLength());
2.UNICODE
CString str1 = _T("123");
int len = WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL);
char *ptxtTemp = new char[len+1];
WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL);
//.......
delete[] ptxtTemp;
char*转CString
char *str="aaaa";
CString str1(str);
3.CString和string
CString转string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间
string转CString
CString.format("%s",string.c_str());
4.CString和int
CString转int
宽字节下:
CString str1="123";
int i = atoi(str1);
UNICODE:
CString str2 = _T("100");
int i;
swscanf(str2,_T("%d"),&i);
int转CString
int i = 100;
CString str;
str.Format("%d",i);
5.CString和LPCWSTR
CString转LPCWSTR
方法1:
CString strFileName;
LPCWSTR lpcwStr = strFileName.AllocSysString();
方法2:
CString str=_T("TestStr");
USES_CONVERSION;
LPCWSTR lpcwStr = A2CW((LPCSTR)str);
LPCWSTR转CString
LPCWSTR lpcwStr = L"TestWStr";
CString str(lpcwStr);
CString str;
LPWSTR lpstr = (LPWSTR)(LPCWSTR)str;
6.CString和LPSTR
CString转LPSTR
方法1:
CString strFileName;
LPSTR lpStr = strFileName.GetBuffer();
strFileName.ReleaseBuffer();
方法2:
LPSTR lpStr = (LPSTR)(LPCSTR)strFimeName;
LPSTR转CString
LPSTR lpStr = L"TestStr";
CString str(lpStr);
7.CString和BSTR
CString转BSTR
CString str;
str = .....; // whatever
BSTR bStr = str.AllocSysString();
BSTR转CString
UNICODE下:
CString convert(BSTR bStr)
{
if(bStr == NULL)
return CString(_T(""));
CString s(bStr); // in UNICODE mode
<span> </span>return s;
}
宽字节下:
CString convert(BSTR b)
{
CString s;
if(b == NULL)
<span style="white-space:pre"> </span> return s; // empty for NULL BSTR
#ifdef UNICODE
s = b;
#else
LPSTR p = s.GetBuffer(SysStringLen(b) + 1);
::WideCharToMultiByte(CP_ACP, // ANSI Code Page
0, // no flags
b, // source widechar string
-1, // assume NUL-terminated
p, // target buffer
SysStringLen(b)+1, // target buffer length
NULL, // use system default char
NULL); // don''t care if default used
s.ReleaseBuffer();
#endif
return s;
}
8.char*和LPCWSTR
char*转LPCWSTR
1、通过MultiByteToWideChar函数转换 view plain co
char* szStr = "测试字符串";
WCHAR wszClassName[256];
memset(wszClassName,0,sizeof(wszClassName));
MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
sizeof(wszClassName)/sizeof(wszClassName[0]));
2、通过T2W转换宏
char* szStr = "测试字符串";
CString str = CString(szStr);
USES_CONVERSION;
LPCWSTR wszClassName = new WCHAR[str.GetLength()+1];
wcscpy((LPTSTR)wszClassName,T2W((LPTSTR)str.GetBuffer(NULL)));
str.ReleaseBuffer();
3、通过A2CW转
char* szStr = "测试字符串";
CString str = CString(szStr);
USES_CONVERSION;
LPCWSTR wszClassName = A2CW(W2A(str));
str.ReleaseBuffer();
以上是在UNICODE状态下