CString与char之间的转换
1、多字符集設置下:
CString To char*:
1)、
CString str;?
char buf = str.GetBuffer();
2)、
CString str;
char buf = (LPSTR)(LPCSTR)str;
char To CString
1)、
char buf;
CString str(buf);
2、Unicode字符集下:
CString To char:
1)、
CString st =_T(“123”);??
int len =WideCharToMultiByte(CP_ACP,0,str,-1,NULL,0,NULL,NULL);??
char buf =new char[len +1];??
WideCharToMultiByte(CP_ACP,0,str,-1,buf,len,NULL,NULL );??
??
//…??
delete[] buf;
char To CString
1)、
char buf;
CString str(buf);
3、方法:
static std::string Unicode2ANSI(LPCWSTR lpszSrc)
{
std::string sResult;
if (lpszSrc != NULL)
{
int? nANSILen = WideCharToMultiByte(CP_ACP, 0, lpszSrc, -1, NULL, 0, NULL, NULL);
char pANSI = new char[nANSILen + 1];
if (pANSI != NULL)
{
ZeroMemory(pANSI, nANSILen + 1);
WideCharToMultiByte(CP_ACP, 0, lpszSrc, -1, pANSI, nANSILen, NULL, NULL);
sResult = pANSI;
delete[] pANSI;
}
}
return sResult;
}
static std::wstring ANSI2Unicode(LPCSTR lpszSrc)
{
std::wstring sResult;
if (lpszSrc != NULL)
{
int nUnicodeLen = MultiByteToWideChar(CP_ACP, 0, lpszSrc, -1, NULL, 0);
LPWSTR pUnicode = new WCHAR[nUnicodeLen + 1];
if (pUnicode != NULL)
{
ZeroMemory((void)pUnicode, (nUnicodeLen + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpszSrc,-1, pUnicode, nUnicodeLen);
sResult = pUnicode;
delete[] pUnicode;
}
}
return sResult;
}
總結
以上是生活随笔為你收集整理的CString与char之间的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF之Binding深入探讨
- 下一篇: 袁老走好,谢谢您!我辈也当自强。