About conversion of characters in C + +

Source: Internet
Author: User

In the engineering properties of VS we often set character set:

In order to support Unicode encoding, it is necessary to convert between multibyte and wide bytes. There are two functions involved:

int  widechartomultibyte(  _in_      UINT    CodePage,  _in_      DWORD   dwFlags,  _in_      lpcwstr lpwidecharstr,  _in_      int     cchwidechar,  _out_opt_ LPSTR   lpmultibytestr,  _in_      int     cbmultibyte,  _in_opt_  LPCSTR  Lpdefaultchar,  _out_opt_ lpbool  lpuseddefaultchar);

And

int  MultiByteToWideChar(  _in_      UINT   CodePage,  _in_      DWORD  dwFlags,  _in_      LPCSTR lpmultibytestr,  _in_      int    cbmultibyte,  _out_opt_ lpwstr Lpwidecharstr,  _in_      int    cchwidechar);

For more information see: Https://msdn.microsoft.com/en-us/library/dd374130%28VS.85%29.aspx and https://msdn.microsoft.com/en-us/ Library/windows/desktop/dd319072%28v=vs.85%29.aspx

These two functions need to specify code pages when they are used, and commonly used code pages are made up of CP_ACP and Cp_utf8 two. The conversion between ANSI and Unicode is achieved using the CP_ACP code page, and the conversion between UTF-8 and Unicode is achieved using the Cp_utf8 code page.

Example:

#include <iostream>#include<string>#include<windows.h>std::wstring Ansitounicode (ConstSTD::string&str); std::stringUnicodetoansi (Conststd::wstring&str); std::wstring Utf8tounicode (ConstSTD::string&str); std::stringUnicodeToUTF8 (Conststd::wstring&str);intMain () {std::stringstr ="Hello world!"; Std::wstring Wstr=Ansitounicode (str); Std::wstring WSTR1= L"Hello World"; STD::stringSTR1 =Unicodetoansi (WSTR1); System ("Pause"); return 0;} Std::wstring Ansitounicode (ConstSTD::string&str) {    intLen =0; Len=str.length (); intUnicodelen =:: MultiByteToWideChar (CP_ACP,//the first time just to get the length converted to wide characters        0, Str.c_str (),-1, NULL,0); wchar_t*Punicode; Punicode=NewWchar_t[unicodelen +1]; memset (Punicode,0, (Unicodelen +1)*sizeof(wchar_t)); :: MultiByteToWideChar (CP_ACP,0, Str.c_str (),-1, (LPWSTR) Punicode, Unicodelen);    std::wstring RT; RT= (wchar_t*) Punicode; DeletePunicode; returnRT;} STD::stringUnicodetoansi (Conststd::wstring&str) {    Char*Pelementtext; intItextlen; //wide char to multi charItextlen =WideCharToMultiByte (CP_ACP,0, Str.c_str (),-1, NULL,0, NULL, NULL); Pelementtext=New Char[Itextlen +1]; memset ((void*) Pelementtext,0,sizeof(Char) * (Itextlen +1)); :: WideCharToMultiByte (CP_ACP,0, Str.c_str (),-1, Pelementtext, Itextlen, NULL, and NULL); STD::stringStrText; StrText=Pelementtext; Delete[] pelementtext; returnStrText;} Std::wstring Utf8tounicode (ConstSTD::string&str) {    intLen =0; Len=str.length (); intUnicodelen =:: MultiByteToWideChar (Cp_utf8,0, Str.c_str (),-1, NULL,0); wchar_t*Punicode; Punicode=NewWchar_t[unicodelen +1]; memset (Punicode,0, (Unicodelen +1)*sizeof(wchar_t)); :: MultiByteToWideChar (Cp_utf8,0, Str.c_str (),-1, (LPWSTR) Punicode, Unicodelen);    std::wstring RT; RT= (wchar_t*) Punicode; DeletePunicode; returnRT;} STD::stringUnicodeToUTF8 (Conststd::wstring&str) {    Char*Pelementtext; intItextlen; //wide char to multi charItextlen =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, and NULL); STD::stringStrText; StrText=Pelementtext; Delete[] pelementtext; returnStrText;}

Results:

There is also a way to use the C function library:

#include <string>#include<iostream>#include<cstdlib>std::stringWs2s (Conststd::wstring&ws) {std::stringCurlocale = setlocale (Lc_all, NULL);//Curlocale = "C";SetLocale (Lc_all,"CHS");//Chinese Area    Constwchar_t* _source =Ws.c_str (); size_t _dsize= Ws.size () +1;//1 Larger than the number of wide characters to convert    Char*_dest =New Char[_dsize]; memset (_dest,0, _dsize);//Initializing bufferssize_t i; wcstombs_s (&I, _dest, _dsize, _source, Ws.size ()); Std::cout<<"I:"<< I <<Std::endl; STD::stringresult =_dest; Delete[]_dest;                               SetLocale (Lc_all, Curlocale.c_str ()); //set back to the original locale    returnresult;} Std::wstring S2ws (ConstSTD::string&s) {std::stringCurllocale =setlocale (Lc_all, NULL); SetLocale (Lc_all,"CHS"); Const Char* _source =S.c_str (); size_t _dsize= S.size () +1; wchar_t* _dest =NewWchar_t[_dsize];    size_t i; mbstowcs_s (&I, _dest, _dsize, _source, S.size ()); std::wstring result=_dest; Delete[] _dest;    SetLocale (Lc_all, Curllocale.c_str ()); returnresult;}intMain () {std::wstring wstr= L"Hello world!"; STD::stringstr =ws2s (WSTR); STD::stringSTR1 ="Hello world!"; Std::wstring WSTR1=S2ws (str); System ("Pause"); return 0;}

Results:

The third method:

#include <string>#include<iostream>std::wstring stringtowstring (ConstSTD::string&str) {std::wstring wstr (str.length (), L' ');    Std::copy (Str.begin (), Str.end (), Wstr.begin ()); returnwstr;}//Copy only low-byte to stringSTD::stringWstringtostring (ConstStd::wstring &wstr) {std::stringSTR (wstr.length (),' ');    Std::copy (Wstr.begin (), Wstr.end (), Str.begin ()); Std::cout<< Str <<Std::endl; returnstr;}intMain () {std::wstring wstr= L"Hello world!"; STD::stringstr =wstringtostring (WSTR); STD::stringSTR1 ="Hello world!"; Std::wstring WSTR1=stringtowstring (str); System ("Pause"); return 0;}

Results:

About conversion of characters in C + +

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.