windows 编码
这里的多字节,通常对应vs的多字节,一般就是gbk和ansi
bool Unicode2UTF(vector<char> &Dest, wchar_t *szSrc)
{
if(szSrc == NULL)
return false;
int iTextLen = wcslen(szSrc);
if (iTextLen == 0)
{
return false;
}
char* pElementText;
iTextLen = WideCharToMultiByte( CP_UTF8,
0,
(LPWSTR)szSrc,
-1,
NULL,
0,
NULL,
NULL );
if(iTextLen==0)
return false;
Dest.resize(iTextLen);
::WideCharToMultiByte( CP_UTF8,
0,
(LPWSTR)szSrc,
-1,
&*Dest.begin(),
iTextLen,
NULL,
NULL );
return true;
}
bool UTF2Unicode(vector<wchar_t> &Dest,const char *szSrc)
{
int iTextLen=strlen(szSrc);
if(iTextLen == 0)
{
return false;
}
char* pElementText;
//MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
iTextLen = MultiByteToWideChar(CP_UTF8, NULL, szSrc, strlen(szSrc),NULL,0 );
if(iTextLen==0)
return false;
Dest.resize(iTextLen);
::MultiByteToWideChar (CP_UTF8, 0, szSrc, -1, (LPWSTR)&*Dest.begin(), iTextLen);
return true;
}
// 多字节编码转为Unicode编码
bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int mLen)
{
// convert an MBCS string to widechar
int uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
if (uLen<=0)
{
return false;
}
pun.resize(uLen);
int nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen);
if (nRtn != uLen)
{
pun.clear();
return false;
}
return true;
}
//Unicode编码转为多字节编码
bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int uLen)
{
// convert an widechar string to Multibyte
int MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL);
if (MBLen <=0)
{
return false;
}
pmb.resize(MBLen);
int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL);
if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
}