这东西网上大把抓,自己常用的几个函数放上来做个备忘。
#include <locale.h>
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
size_t DesLen;
wcstombs_s(&DesLen, _Dest, _Dsize, _Source, _Dsize);
std::string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
std::wstring s2ws(const std::string& s)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
size_t DesLen;
mbstowcs_s(&DesLen, _Dest, _Dsize, _Source, _Dsize);
std::wstring result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
void GbToUtf8(char* pstrOut, int dwOutLen, const char* pstrIn, int dwInLen)
{
#ifdef WIN32
int i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
wchar_t * strSrc = new wchar_t[i + 1];
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, strSrc, i);
i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
#else
iconv_t iConv = iconv_open("utf-8", "gb2312");
iconv(iConv, (char **)&pstrIn, (size_t *)&dwInLen, (char **)&pstrOut, (size_t *)&dwOutLen);
iconv_close(iConv);
#endif
}
void Utf8ToGb(char* pstrOut, unsigned short dwOutLen, const char* pstrIn, unsigned short dwInLen)
{
if (NULL == pstrOut)
{
return;
}
#ifdef WIN32
int i = MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, NULL, 0);
wchar_t * strSrc = new wchar_t[i + 1];
MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, strSrc, i);
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
#else
iconv_t iConv = iconv_open("gb2312", "utf-8");
iconv(iConv, (char **)&pstrIn, (size_t *)&dwInLen, (char **)&pstrOut, (size_t *)&dwOutLen);
iconv_close(iConv);
#endif
}
void GbToUnicode(wchar_t* pstrOut, unsigned short dwOutLen, const char* pstrIn, unsigned short dwInLen)
{
if (NULL == pstrOut)
{
return;
}
#ifdef WIN32
unsigned short i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, pstrOut, i);
#else
iconv_t iConv = iconv_open("unicode", "gb2312");
iconv(iConv, (char**)pstrIn, &dwInLen, (char**)pstrOut, &dwOutLen);
iconv_close(iConv);
#endif
}
void UnicodeToGb(char* pstrOut, unsigned short dwOutLen, const wchar_t* pstrIn, unsigned short dwInLen)
{
if (NULL == pstrOut)
{
return;
}
#ifdef WIN32
unsigned short i = WideCharToMultiByte(CP_ACP, 0, pstrIn, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_ACP, 0, pstrIn, -1, pstrOut, i, 0, 0);
#else
iconv_t iConv = iconv_open("gb2312", "unicode");
iconv(iConv, (char**)pstrIn, &dwInLen, (char**)pstrOut, &dwOutLen);
iconv_close(iConv);
#endif
}
void Utf8ToUnicode(wchar_t* pstrOut, unsigned short dwOutLen, const char* pstrIn, unsigned short dwInLen)
{
if (NULL == pstrOut)
{
return;
}
#ifdef WIN32
unsigned short i = MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, NULL, 0);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, pstrOut, i);
#else
iconv_t iConv = iconv_open("unicode", "utf-8");
iconv(iConv, (char**)pstrIn, &dwInLen, (char**)pstrOut, &dwOutLen);
iconv_close(iConv);
#endif
}
std::wstring Utf82ws(const char* pstrIn)
{
if (NULL == pstrIn)
{
return std::wstring(_T(""));
}
unsigned short i = MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, NULL, 0);
wchar_t * pstrDst = new wchar_t[i];
ZeroMemory(pstrDst, sizeof(wchar_t)*i);
MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, pstrDst, i);
std::wstring strResult = pstrDst;
delete[]pstrDst;
return strResult;
}
std::wstring Gb2ws(const char* pstrIn)
{
if (NULL == pstrIn)
{
return std::wstring(_T(""));
}
unsigned short i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
wchar_t * pstrDst = new wchar_t[i];
ZeroMemory(pstrDst, sizeof(wchar_t)*i);
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, pstrDst, i);
std::wstring strResult = pstrDst;
delete[]pstrDst;
return strResult;
}