C + + ANSI, UNICODE, UTF8 Mutual transfer

Source: Internet
Author: User


Reprinted from:http://www.cppblog.com/Tongy0/archive/2013/03/13/198378.html

StaticStd::wstring mbytestowstring (Const Char*lpcszstring); StaticSTD::stringWstringtombytes (Constwchar_t*lpwcszwstring); StaticStd::wstring utf8towstring (Const Char*lpcszstring); StaticSTD::stringWStringToUTF8 (Constwchar_t*lpwcszwstring); std::wstring kklogobject::mbytestowstring (Const Char*lpcszstring) { intLen =strlen (lpcszstring); intUnicodelen =:: MultiByteToWideChar (CP_ACP,0, Lpcszstring,-1Null0); wchar_t* Punicode =NewWchar_t[unicodelen +1]; memset (Punicode,0, (Unicodelen +1) *sizeof(wchar_t)); :: MultiByteToWideChar (CP_ACP,0, Lpcszstring,-1, (LPWSTR) Punicode, Unicodelen); Wstring wstring= (wchar_t*) Punicode; Delete[] punicode; returnwString;} STD::stringKklogobject::wstringtombytes (Constwchar_t*lpwcszwstring) { Char*Pelementtext; intItextlen; //wide char to multi charItextlen =:: WideCharToMultiByte (CP_ACP,0, Lpwcszwstring,-1Null0, NULL, NULL); Pelementtext=New Char[Itextlen +1]; memset ((void*) Pelementtext,0, (Itextlen +1) *sizeof(Char)); :: WideCharToMultiByte (CP_ACP,0, Lpwcszwstring,0, Pelementtext, Itextlen, NULL, and NULL); STD::stringStrreturn (Pelementtext); Delete[] pelementtext; returnStrreturn;} Std::wstring kklogobject::utf8towstring (Const Char*lpcszstring) { intLen =strlen (lpcszstring); intUnicodelen =:: MultiByteToWideChar (Cp_utf8,0, Lpcszstring,-1Null0); wchar_t*Punicode; Punicode=NewWchar_t[unicodelen +1]; memset ((void*) Punicode,0, (Unicodelen +1) *sizeof(wchar_t)); :: MultiByteToWideChar (Cp_utf8,0, Lpcszstring,-1, (LPWSTR) Punicode, Unicodelen); Wstring Wstrreturn (Punicode); Delete[] punicode; returnWstrreturn;} STD::stringKklogobject::wstringtoutf8 (Constwchar_t*lpwcszwstring) { Char*Pelementtext; intItextlen =:: WideCharToMultiByte (Cp_utf8,0, (LPWSTR) lpwcszwstring,-1Null0, NULL, NULL); Pelementtext=New Char[Itextlen +1]; memset ((void*) Pelementtext,0, (Itextlen +1) *sizeof(Char)); :: WideCharToMultiByte (Cp_utf8,0, (LPWSTR) lpwcszwstring,-1, Pelementtext, Itextlen, NULL, and NULL); STD::stringStrreturn (Pelementtext); Delete[] pelementtext; returnStrreturn;}

C + + ANSI, UNICODE, UTF8 Mutual transfer

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.