http://blog.csdn.net/zaccheodong/article/details/6951868
- // ansi在本机是MBCS(实际上是DBCX) 转为UNIcode默认的UTF16编码
- std::wstring ANSI_2_UTF16( const string& strANSI )
- {
- int nUnicodeLength = ::MultiByteToWideChar(CP_ACP,0,strANSI.c_str(),-1,NULL,0) ;
- wstring strUTF16(nUnicodeLength,_T(' '));
- int nRet = ::MultiByteToWideChar(CP_ACP,0,strANSI.c_str(),-1,&strUTF16[0],nUnicodeLength);
- ASSERT(0 != nRet);
- return strUTF16;
- }
- // UNIcode默认的UTF16编码转为 ansi 在本机是MBCS(实际上是DBCX)
- std::string UTF16_2_ANSI( const wstring& strUTF16 )
- {
- int nANSILength = ::WideCharToMultiByte(CP_ACP,0,strUTF16.c_str(),-1,NULL,0,0,0);
- string strANSI(nANSILength,' ');
- int nRet = ::WideCharToMultiByte(CP_ACP,0,strUTF16.c_str(),-1,&strANSI[0],nANSILength,0,0);
- ASSERT(0 != nRet);
- return strANSI;
- }
- // UNIcode默认的UTF16编码转为 UTF8编码
- std::string UTF16_2_UTF8( const wstring& strUTF16 )
- {
- int nUTF8Length = ::WideCharToMultiByte(CP_UTF8,
- 0,
- strUTF16.c_str(),
- -1,
- NULL,
- 0,
- 0,0);
- string strUTF8(nUTF8Length+1,'\0');
- int nRet = ::WideCharToMultiByte(CP_UTF8,
- 0,
- strUTF16.c_str(),
- -1,
- &strUTF8[0],
- nUTF8Length+1,
- 0,
- 0);
- return strUTF8;
- }
- // UNIcode UTF8编码 转为 UNIcode默认的UTF16编码
- std::wstring UTF8_2_UTF16( const string& strUTF8 )
- {
- int nUTF16Length = ::MultiByteToWideChar(CP_UTF8,0,strUTF8.c_str(),-1,NULL,0);
- nUTF16Length += 1;
- wstring strUTF16(nUTF16Length ,' ');
- int nRet = ::MultiByteToWideChar(CP_UTF8,0,strUTF8.c_str(),-1,
- &strUTF16[0],nUTF16Length);
- ASSERT(0 != nRet);
- return strUTF16;
- }
- //UTF8 编码的UNICODE字符集转为ANSI编码,先转换为UTF16编码,再转换为ANSI编码
- std::string UTF8_2_ANSI( const string& strUTF8 )
- {
- wstring wstrUTF16 = UTF8_2_UTF16(strUTF8);
- string strANSI = UTF16_2_ANSI(wstrUTF16);
- return strANSI;
- }
- //ANSI编码 转为 UTF8编码的,先转换为UTF16编码,再转换为UTF8编码
- std::string ANSI_2_UTF8( const string& strANSI )
- {
- wstring wstrUTF16 = ANSI_2_UTF16(strANSI);
- string strUTF8 = UTF16_2_UTF8(wstrUTF16);
- return strUTF8;
- }