Character encoding conversion (using the Windows API)

Source: Internet
Author: User

Static std::wstring utf8towstring (const std::string& stext)
{
int nlenwidecharstr = MultiByteToWideChar (Cp_utf8, 0, Stext.c_str (),-1, NULL, 0);
Pwchar pwidecharstr = NULL;
Pwidecharstr = (pwstr) HeapAlloc (GetProcessHeap (), 0, nlenwidecharstr*sizeof (wchar_t));
memset (pwidecharstr,0,nlenwidecharstr*sizeof (wchar_t));
MultiByteToWideChar (Cp_utf8, 0, Stext.c_str (),-1, Pwidecharstr, NLENWIDECHARSTR);
std::wstring widestr = pwidecharstr;
HeapFree (GetProcessHeap (), 0, PWIDECHARSTR);
return widestr;
}

  static std::string WStringToUtf8 (const std::wstring& stext)
  {
    int nlenwidecharstr = WideCharToMultiByte (Cp_utf8, 0, Stext.c_str (),-1, NULL, 0, NULL, NULL);
   pchar pcharstr = NULL;
   pcharstr = (PSTR) HeapAlloc (GetProcessHeap (), 0, nlenwidecharstr*sizeof (char));
   memset (pcharstr,0,nlenwidecharstr*sizeof (char));
   widechartomultibyte (Cp_utf8, 0, Stext.c_str (), 1, pcharstr, nlenwidecharstr, NULL, NULL);
   std::string str = PCHARSTR;
   heapfree (GetProcessHeap (), 0, PCHARSTR);
   return str;
  }

  static std::string wstringtoansi (const std::wstring& stext)
  {
    int nlenwidecharstr = WideCharToMultiByte (CP_ACP, 0, Stext.c_str (),-1, NULL, 0, NULL, NULL);
   pchar pcharstr = NULL;
   pcharstr = (PSTR) HeapAlloc (GetProcessHeap (), 0, nlenwidecharstr*sizeof (char));
   memset (pcharstr,0,nlenwidecharstr*sizeof (char));
   widechartomultibyte (CP_ACP, 0, Stext.c_str (), 1, pcharstr, nlenwidecharstr, NULL, NULL);
   std::string str = PCHARSTR;
   heapfree (GetProcessHeap (), 0, PCHARSTR);
   return str;
  }

Static std::wstring ansitowstring (const std::string& stext)
{
int nlenwidecharstr = MultiByteToWideChar (CP_ACP, 0, Stext.c_str (),-1, NULL, 0);
Pwchar pwidecharstr = NULL;
Pwidecharstr = (pwstr) HeapAlloc (GetProcessHeap (), 0, nlenwidecharstr*sizeof (wchar_t));
memset (pwidecharstr,0,nlenwidecharstr*sizeof (wchar_t));
MultiByteToWideChar (CP_ACP, 0, Stext.c_str (),-1, Pwidecharstr, NLENWIDECHARSTR);
std::wstring widestr = pwidecharstr;
HeapFree (GetProcessHeap (), 0, PWIDECHARSTR);
return widestr;

}

Static std::string utf8toansi (const std::string& stext)
{
std::wstring ws = Utf8towstring (stext);
return Wstringtoansi (WS);
}

Static std::string AnsiToUtf8 (const std::string& stext)
{
std::wstring ws = Ansitowstring (stext);
return WStringToUtf8 (WS);
}

Character encoding conversion (using the Windows API)

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.