const Int CHAR_BUFSIZE = 4096; /**< 待转换名最大长度*/
/** GB2312转UTF8,假定pcUtf8已有足够空间*/
__inline void Gb2312ToUtf8(const char *pcGb2312, std::string & strUtf8)
{
//wchar_t pcUnicode[CHAR_BUFSIZE];
//gb2312 to unicode
Int nUnicodeLen = MultiByteToWideChar(CP_ACP, 0, pcGb2312, -1, NULL, 0);
wchar_t * pcUnicode = new wchar_t[nUnicodeLen+1];
memset(pcUnicode, 0, nUnicodeLen * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, pcGb2312, -1, pcUnicode, nUnicodeLen);
//unicode to utf8
Int nUtf8Len = WideCharToMultiByte(CP_UTF8, 0, pcUnicode, -1, NULL, 0, NULL, NULL);
char *pcUtf8=new char[nUtf8Len + 1];
memset(pcUtf8, 0, nUtf8Len + 1);
WideCharToMultiByte(CP_UTF8, 0, pcUnicode, -1, pcUtf8, nUtf8Len, NULL, NULL);
strUtf8=pcUtf8;
delete[] pcUtf8;
delete[] pcUnicode;
}
/** UTF8转GB2312,假定pcGb2312已有足够空间*/
__inline void Utf8ToGb2312(const char *pcUtf8, std::string & strGB2312)
{
//wchar_t pcUnicode[CHAR_BUFSIZE];
Int nUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, pcUtf8, -1, NULL, 0);
wchar_t * pcUnicode = new wchar_t[nUnicodeLen+1];
memset(pcUnicode, 0, nUnicodeLen * 2 + 2);
//utf8 to unicode
MultiByteToWideChar(CP_UTF8, 0, pcUtf8, -1, pcUnicode, nUnicodeLen);
//unicode to gb2312
int nGb2312Len = WideCharToMultiByte(CP_ACP, 0, pcUnicode, -1, NULL, 0, NULL, NULL);
char *pcGb2312=new char[nGb2312Len + 1];
memset(pcGb2312, 0, nGb2312Len + 1);
WideCharToMultiByte(CP_ACP, 0, pcUnicode, -1, pcGb2312, nGb2312Len, NULL, NULL);
strGB2312=pcGb2312;
delete[] pcGb2312;
delete[] pcUnicode;
}
__inline std::string StringToUtf8(const std::string& strSource)
{
std::string strTmp="";
Gb2312ToUtf8(strSource.c_str(),strTmp);
return strTmp;
}
__inline std::string StringToGB2312(const std::string& strSource)
{
std::string strTmp="";
Utf8ToGb2312(strSource.c_str(),strTmp);
return strTmp;
}
__inline std::string StringToUtf8(const char * strSource)
{
return StringToUtf8(std::string(strSource));
}
__inline std::string StringToGB2312(const char * strSource)
{
std::string strTmp="";
Utf8ToGb2312(strSource,strTmp);
return strTmp;
}