Returns the conversion functions of ANSI, Unicode, and utf8.

Source: Internet
Author: User

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 ));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.