The conversion between multibyte and UTF-8, Unicode

Source: Internet
Author: User

Multibyte encoding to UTF8 encoded BOOL MBToUTF8 (vector<char>& pu8, const char* PMB, Int32 mlen) {//Convert an MBCS string to WI Dechar Int32 nlen = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, NULL, 0); wchar* LPSZW = NULL; try {lpszw = new Wchar[nlen];} catch (Bad_alloc &memexp) {return false;} int32 Nrtn = MultiByteToWideChar (CP_ACP, 0 , PMB, Mlen, LPSZW, Nlen); if (Nrtn!= nlen) {delete[] lpszw; return false;}//Convert a Widechar string to utf8 int32 utf8len = Widechartomultiby Te (Cp_utf8, 0, LPSZW, nlen, NULL, 0, NULL, NULL); if (Utf8len <= 0) {return false;} pu8.resize (Utf8len); Nrtn = WideCharToMultiByte (Cp_utf8, 0, LPSZW, Nlen, &*pu8.begin (), utf8len, NULL, NULL); Delete[] LPSZW; if (Nrtn!= utf8len) {pu8.clear (); return false;} return true; }//UTF8 encoding to multibyte-encoded bool Utf8tomb (vector<char>& PMB, const char* Pu8, Int32 utf8len) {//Convert an UTF8 string To widechar int32 nlen = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, NULL, 0); wchar* LPSZW = NULL; try {LPSZW = new Wchar[nlen]; catch (Bad_alloc &memexp) {return false;} int32 Nrtn = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, LPSZW, Nlen); if (Nrtn!= nlen) {delete[] lpszw; return false;}//Convert a Widechar string to multibyte int32 mblen = Widechartomult Ibyte (CP_ACP, 0, LPSZW, nlen, NULL, 0, NULL, NULL); if (Mblen <=0) {return false;} pmb.resize (Mblen); Nrtn = WideCharToMultiByte (CP_ACP, 0, LPSZW, Nlen, &*pmb.begin (), mblen, NULL, NULL); Delete[] LPSZW; if (Nrtn!= mblen) {pmb.clear (); return false;} return true; //multibyte encoding to Unicode encoding bool Mbtounicode (vector<wchar_t>& pun, const char* PMB, Int32 mlen) {//convert an MBCS s Tring to widechar int32 ulen = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, NULL, 0); if (ulen<=0) {return false;} pun.resize (Ulen); Int32 nrtn = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, &*pun.begin (), Ulen); if (Nrtn!= ulen) {pun.clear (); return false;} return true; //unicode encoding to multibyte-encoded bool Unicodetomb (vector<char>& PMB, const wchar_t* Pun, Int32 ulen) {//Convert an Widechar string to multibyte int32 mblen = WideCharToMultiByte ( CP_ACP, 0, pun, ulen, NULL, 0, NULL, NULL); if (Mblen <=0) {return false;} pmb.resize (Mblen); int nrtn = WideCharToMultiByte (CP_ACP, 0, Pun, Ulen, &*pmb.begin (), mblen, NULL, NULL); if (Nrtn!= mblen) {pmb.clear (); return false;} return true; }//UTF8 encoding to Unicode bool Utf8tounicode (vector<wchar_t>& pun, const char* Pu8, Int32 utf8len) {//Convert an UT F8 string to widechar int32 nlen = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, NULL, 0); if (Nlen <=0) {return false;} pun.resize (Nlen); Int32 nrtn = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, &*pun.begin (), Nlen); if (Nrtn!= nlen) {pun.clear (); return false;} return true; }//Unicode encoding to UTF8 bool UnicodeToUTF8 (vector<char>& pu8, const wchar_t* Pun, Int32 ulen) {//Convert an Widec Har string to utf8 int32 utf8len = WideCharToMultiByte (Cp_utf8, 0, pun, ulen, NULL, 0, NULL, NULL); if (utf8len<=0) {return false;} pu8.resize (Utf8len); Int32 nrtn = WideCharToMultiByte (Cp_utf8, 0, Pun, Ulen, &*pu8.begin (), utf8len, NULL, NULL); if (Nrtn!= utf8len) {pu8.clear (); return false;} return true; }

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.