C + + implementation of UTF-8 encoding and Unicode encoding mutual transfer __HTML5

Source: Internet
Author: User

Turn from: http://www.itstrike.cn/Question/5e977ab5-e035-4595-a35b-91aae63fa394.html

1.utf-8 Turn Unicode

Wstring utf8tounicode (const string& str)
{
 int  len = 0;
 Len = Str.length ();
 int  Unicodelen =:: MultiByteToWideChar (Cp_utf8,
            0,
            str.c_str (),
            -1,
            NULL,
            0); 
 wchar_t *  Punicode; 
 Punicode = new  wchar_t[unicodelen+1]; 
 Memset (punicode,0, (unicodelen+1) *sizeof (wchar_t)); 
 :: MultiByteToWideChar (Cp_utf8,
         0,
         str.c_str ( ),
         -1,
         (LPWSTR) Punicode,
         Unicodelen); 
 Wstring  RT; 
 RT = (wchar_t*) punicode;
 Delete  Punicode;

 return  RT; 
}

2.Unicode Turn UTF-8

string UnicodeToUTF8 (const wstring& str) {char* pelementtext;
 int Itextlen;
         Wide char to multi char Itextlen = WideCharToMultiByte (Cp_utf8, 0, Str.c_str (),-1,
 NULL, 0, NULL, NULL);
 Pelementtext = new Char[itextlen + 1];
 memset ((void*) pelementtext, 0, sizeof (char) * (Itextlen + 1));
         :: WideCharToMultiByte (Cp_utf8, 0, Str.c_str (),-1, Pelementtext, Itextlen,
 NULL, NULL);
 String StrText;
 StrText = Pelementtext;
 Delete[] Pelementtext;
return strText; }

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.