////////////////////////////////////////////////////////////////////////// // kazi add here [8/26/2004-18:48]
std::string Transcode(const wchar_t *pwszString, unsigned int nCodePage /* = CP_UTF8 */) { std::string strRet; int nReqLen = WideCharToMultiByte(nCodePage,0,pwszString,wcslen(pwszString),0,0,0,0); // max possible length of converted 8bit string
char *pszDst = new char[nReqLen + 1];
// get string int nLen = WideCharToMultiByte(nCodePage,0,pwszString,wcslen(pwszString),pszDst,nReqLen,0,0);
if (nLen){ pszDst[nLen] = 0; // null terminator strRet = pszDst; // copy to STL string } // if (nLen)
delete[] pszDst; // delete, if allocated
return strRet; }
std::string Transcode(const char *pwszString, unsigned int nCodePage /* = CP_ACP */) { std::string strRet; int nReqLen = MultiByteToWideChar( nCodePage, 0, pwszString, strlen(pwszString)+1, 0, 0 );
// allocate buffer wchar_t *pszDst = new wchar_t[nReqLen + 1]; //initially, point to array
// get string //int nLen = MultiByteToWideChar(nCodePage,0,pwszString,nRawLen,pszDst,nReqLen); int nLen = MultiByteToWideChar( nCodePage, 0, pwszString, strlen(pwszString)+1, pszDst, nReqLen );
if (nLen){ pszDst[nLen] = 0; // null terminator //strRet = pszDst; // copy to STL string strRet = Transcode( pszDst, nCodePage == CP_ACP ? CP_UTF8 : CP_ACP ); // copy to STL string } // if (nLen)
delete[] pszDst; // delete, if allocated return strRet; } /////////////////////////////////////////////////////
//i use the function like this: // Set string value: item.SetAttribute( "Name", Transcode("服务器任务") .c_str() ); // Read string value: strcpy( szTmp, Transcode( pEle->Attribute("Name"), CP_UTF8 ).c_str() );
////////////////////////////////////////////////////////////////////////// I think you can try this methord.
Log in to post a comment.
//////////////////////////////////////////////////////////////////////////
// kazi add here [8/26/2004-18:48]
std::string Transcode(const wchar_t *pwszString, unsigned int nCodePage /* = CP_UTF8 */)
{
std::string strRet;
int nReqLen = WideCharToMultiByte(nCodePage,0,pwszString,wcslen(pwszString),0,0,0,0); // max possible length of converted 8bit string
char *pszDst = new char[nReqLen + 1];
// get string
int nLen = WideCharToMultiByte(nCodePage,0,pwszString,wcslen(pwszString),pszDst,nReqLen,0,0);
if (nLen){
pszDst[nLen] = 0; // null terminator
strRet = pszDst; // copy to STL string
} // if (nLen)
delete[] pszDst; // delete, if allocated
return strRet;
}
std::string Transcode(const char *pwszString, unsigned int nCodePage /* = CP_ACP */)
{
std::string strRet;
int nReqLen = MultiByteToWideChar( nCodePage, 0, pwszString,
strlen(pwszString)+1, 0, 0 );
// allocate buffer
wchar_t *pszDst = new wchar_t[nReqLen + 1]; //initially, point to array
// get string
//int nLen = MultiByteToWideChar(nCodePage,0,pwszString,nRawLen,pszDst,nReqLen);
int nLen = MultiByteToWideChar( nCodePage, 0, pwszString,
strlen(pwszString)+1, pszDst, nReqLen );
if (nLen){
pszDst[nLen] = 0; // null terminator
//strRet = pszDst; // copy to STL string
strRet = Transcode( pszDst, nCodePage == CP_ACP ? CP_UTF8 : CP_ACP ); // copy to STL string
} // if (nLen)
delete[] pszDst; // delete, if allocated
return strRet;
}
/////////////////////////////////////////////////////
//i use the function like this:
// Set string value:
item.SetAttribute( "Name", Transcode("服务器任务") .c_str() );
// Read string value:
strcpy( szTmp, Transcode( pEle->Attribute("Name"), CP_UTF8 ).c_str() );
//////////////////////////////////////////////////////////////////////////
I think you can try this methord.