Http://stdsoft.blogbus.com/logs/56468290.html
Static wstring ansi2unicode (const string & strin)
{
Wstring Strout;
// Pre-calculate the size of the required space (including the ending character), in the unit of wchar_t
Int dwnum = multibytetowidechar (cp_acp, 0, strin. c_str (),-1, 0, 0 );
Wchar_t * pbuffer = new wchar_t [dwnum];
If (! Pbuffer)
{
Return Strout;
}
Memset (pbuffer, 0, (dwnum) * sizeof (wchar_t ));
If (multibytetowidechar (cp_acp, 0, strin. c_str (),-1, pbuffer, dwnum)> = 0)
{
Strout = pbuffer;
}
Delete [] pbuffer;
Return Strout;
}
Static string unicode2utf8 (const wstring & strin)
{
String Strout;
// Size of the storage space required for testing (including ending characters), In char
Int dwnum = widechartomultibyte (cp_utf8, 0, strin. c_str (),-, 0 );
Char * pbuffer = new char [dwnum];
If (! Pbuffer)
{
Return Strout;
}
Memset (pbuffer, 0, dwnum );
If (widechartomultibyte (cp_utf8, 0, strin. c_str (),-1, pbuffer, dwnum, 0, 0)> = 0)
{
Strout = pbuffer;
}
Delete [] pbuffer;
Return Strout;
}
Static string ansi2utf8 (const string & strin)
{
Return unicode2utf8 (ansi2unicode (strin ));
}
Static string unicode2ansi (const wstring & strin)
{
String Strout;
// Size of the storage space required for testing (including ending characters), In char
Int dwnum = widechartomultibyte (cp_acp, 0, strin. c_str (),-, 0 );
Char * pbuffer = new char [dwnum];
If (! Pbuffer)
{
Return Strout;
}
Memset (pbuffer, 0, dwnum );
bool use_def_char = false;
If (widechartomultibyte (cp_acp, 0, strin. c_str (),-1, pbuffer, dwnum ,"? ", & Use_def_char)> = 0)
{
Strout = pbuffer;
}
Delete [] pbuffer;
Return Strout;
}
Static wstring utf82unicode (const string & strin)
{
Wstring Strout;
// Pre-calculate the size of the required space (including the ending character), in the unit of wchar_t
Int dwnum = multibytetowidechar (cp_utf8, 0, strin. c_str (),-1, 0, 0 );
Wchar_t * pbuffer = new wchar_t [dwnum];
If (! Pbuffer)
{
Return Strout;
}
Memset (pbuffer, 0, dwnum * sizeof (wchar_t ));
If (multibytetowidechar (cp_utf8, 0, strin. c_str (),-1, pbuffer, dwnum)> = 0)
{
Strout = pbuffer;
}
Delete [] pbuffer;
Return Strout;
}
Static string utf82ansi (const string & strin)
{
Return unicode2ansi (utf82unicode (strin ));
}