64,648
社区成员
发帖
与我相关
我的任务
分享
//Converting a WChar string to a Ansi string
std::string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen<= 0) return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
std::string strTemp(pszDst);
delete [] pszDst;
return strTemp;
}
shared_ptr<wstring> UT2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
if (len == 0) return shared_ptr<wstring>(new wstring(L""));
vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
return shared_ptr<wstring>(new wstring(&unicode[0]));
}
shared_ptr<string> WC2UT(const wchar_t* buf)
{
int len = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) return shared_ptr<string>(new string(""));
vector<char> utf8(len);
WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
return shared_ptr<string>(new string(&utf8[0]));
}
shared_ptr<wstring> MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return shared_ptr<wstring>(new wstring(L""));
vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return shared_ptr<wstring>(new wstring(&unicode[0]));
}
shared_ptr<string> WC2MB(const wchar_t* buf)
{
int len = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) return shared_ptr<string>(new string(""));
vector<char> utf8(len);
WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
return shared_ptr<string>(new string(&utf8[0]));
}