64,682
社区成员
发帖
与我相关
我的任务
分享
string stlUnicodeToAscii( LPCWSTR strW )
{
char* pElementText = NULL;
int iTextLen = 0;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
0,
strW,
-1,
NULL,
0,
NULL,
NULL );
if(iTextLen < MAX_CONVER_LEN)
{
pElementText = (char*)_alloca(iTextLen+1);
}
else
{
pElementText = new char[iTextLen+1];
}
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_ACP,
0,
strW,
-1,
pElementText,
iTextLen,
NULL,
NULL );
if(iTextLen < MAX_CONVER_LEN)
{
return string(pElementText);
}
else
{
string tStr(pElementText);
delete [] pElementText;
return tStr;
}
}
CStringA UnicodeToMbcs(LPCWSTR pUnicode)
{
if (pUnicode==NULL)
return "";
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
UINT nLength=wcslen(pUnicode);
int nChar = WideCharToMultiByte(codepage, 0, pUnicode, nLength, 0, 0, 0, 0);
CStringA tempBuffer;
nChar = WideCharToMultiByte(codepage, 0, pUnicode, nLength, tempBuffer.GetBufferSetLength(nChar), nChar, 0, 0);
if( nChar == 0 )
{
return "";
}
tempBuffer.ReleaseBuffer();
return tempBuffer;
}
char szMbcsOut[10];
TCHAR szTszIn[] = _T("Hi!");
sprintf_s(szMbcsOut, sizeof(TCHAR) == 1 ? "%s" : "%S", szTszIn);
sprintf_s(szMbcsOut, sizeof(TCHAR) == 1 ? "%s" : "%S", _T("Hi!"));