charc = ‘a’; //占一个字节
WCHARc = L’a’; //占两个字节
TCHARc = _T(‘a’);
char*str = “abc”; //内存数据为61626300
char*str = “中文abc”; //使用MBCS码
WCHAR*str = L”abc” //内存数据为 //6100620063000000
WCHAR*str = L”中文abc”; //使用UNICODE码
TCHAR*str =_T(“中文abc”);//根据平台自动选择
//字符集
BSTRstr = _bstr_t(“abc”);//内存数据为
//060000006100620063000000
WCHAR*AnsiToUnicode(const char* szStr)
{
int nLen = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, szStr, -1,NULL, 0);
if(nLen ==0) return NULL;
wchar_t* pResult =new wchar_t[nLen];
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, szStr, -1,pResult, nLen);
returnpResult;
}
char*UnicodeToAnsi(const WCHAR* szStr)
{
int nLen = WideCharToMultiByte(CP_ACP,0, szStr, -1,NULL, 0, NULL, NULL);
if(nLen ==0) return NULL;
char*pResult =new char[nLen];
WideCharToMultiByte(CP_ACP,0, szStr, -1,pResult, nLen,NULL, NULL);
returnpResult;
}
WCHAR*pszW = L”abc”;
CW2ApszA(pszW);
char*str = pszA.m_psz;
char*pszA = ”abc”;
CA2WpszW(pszA);
WCHAR*str = pszW.m_psz;
类似的用法还有,CW2T、CA2T、CT2W、CT2A等。
BSTRstr1 = _com_util::ConvertStringToBSTR("abc");
char*str2 = _com_util::ConvertBSTRToString(str1);
#include<string>
std::stringstr1("abc");
std::stringstr2 = "abc";
char*str =(char*)str1.c_str();
WCHAR*与std::wstring之间转换类似处理。
CStringstr1(_T("abc"));
CString str2= _T("abc");
TCHAR*str3 =str1.GetBuffer(10);
int sizeOfBuffer = s.GetAllocLength();
_tcscpy_s(pBuffer, sizeOfBuffer,_T("Hello"));
str1.ReleaseBuffer();
LPCTSTRstr4 = str1;