Ansi utf8 and unicode conversion functions

Source: Internet
Author: User

 

Garbled Code cannot afford to hurt ....

 

The following is the conversion code...

 

Inline wstring utf8ToUnicode (string str)

 

{

// UTF8 to Unicode

 

Size_t wcsLen =: MultiByteToWideChar (CP_UTF8, NULL, str. c_str (), str. length (), NULL, 0 );

// Leave a space for ''to allocate space. MultiByteToWideChar will not give'' Space

Wchar_t * wszString = new wchar_t [wcsLen + 1];

Memset (wszString, 0, sizeof (wchar_t) * (wcsLen + 1 ));

// Conversion

: MultiByteToWideChar (CP_UTF8, NULL, str. c_str (), str. length (), wszString, wcsLen );

 

Wstring wstr = wszString;

Delete wszString;

 

Return wstr;

}

 

Inline string unicode2utf8 (wstring wstr)

{

Int len; www.2cto.com

Len = WideCharToMultiByte (CP_UTF8, 0, wstr. c_str (),-1, NULL, 0, NULL, NULL );

Char * szUtf8 = new char [len * 2 + 2];

Memset (szUtf8, 0, len * 2 + 2 );

WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR) wstr. c_str (),-1, szUtf8, len, NULL, NULL );

String str = szUtf8;

Delete szUtf8;

Return str;

}

From ccSec | cc@insight-labs.org

Related Article

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.