Conversion between string and wstring

Source: Internet
Author: User
Conversion between string and wstring
With vs2003 upgrade to vs2005, many familiar Input and Output Methods and parameter transmission methods are no longer effective (see vs2003 to vs2005 code upgrade key http://bianyongtao.spaces.live.com/blog/cns! Dd6cd3607cce4603! 214. Entry ). The content related to the root string is that wcout is no longer valid. The default parameter transmission mode is changed from char * To wchar_t. To solve these problems, I will provide several conversion methods for C ++ STD: string and STD: wstring to convert each other.
Method 1: Call widechartomultibyte () and multibytetowidechar (). The Code is as follows (for detailed explanations, refer to Windows core programming):
# Include <string>
# Include <windows. h>
Using namespace STD;
// Converting a wchar string to a ANSI string
STD: String wchar2ansi (lpcwstr pwszsrc)
{
Int nlen = widechartomultibyte (cp_acp, 0, pwszsrc,-1, null, 0, null, null );
If (nlen <= 0) return STD: string ("");
Char * pszdst = new char [nlen];
If (null = pszdst) return STD: string ("");
Widechartomultibyte (cp_acp, 0, pwszsrc,-1, pszdst, nlen, null, null );
Pszdst [nlen-1] = 0;
STD: String strtemp (pszdst );
Delete [] pszdst;
Return strtemp;
}
String ws2s (wstring & inputws) {return wchar2ansi (inputws. c_str ());}
// Converting a ANSI string to wchar string
STD: wstring ansi2wchar (lpcstr pszsrc, int nlen)
{
Int nsize = multibytetowidechar (cp_acp, 0, (lpcstr) pszsrc, nlen, 0, 0 );
If (nsize <= 0) return NULL;
Wchar * pwszdst = new wchar [nsize + 1];
If (null = pwszdst) return NULL;
Multibytetowidechar (cp_acp, 0, (lpcstr) pszsrc, nlen, pwszdst, nsize );
Pwszdst [nsize] = 0;
If (pwszdst [0] = 0 xfeff) // skip oxfeff
For (INT I = 0; I <nsize; I ++)
Pwszdst [I] = pwszdst [I + 1];
Wstring wcharstring (pwszdst );
Delete pwszdst;
Return wcharstring;
}
STD: wstring s2ws (const string & S) {return ansi2wchar (S. c_str (), S. Size ());}

Method 2: Use ATL to encapsulate the transition of _ bstr_t: (Note: _ BSTR _ is Microsoft specific, so the following code can be passed in vs2005 without portability );
# Include <string>
# Include <comutil. h>
Using namespace STD;
# Pragma comment (Lib, "comsuppw. lib ")
String ws2s (const wstring & ws );
Wstring s2ws (const string & S );
String ws2s (const wstring & ws)
{
_ Bstr_t T = ws. c_str ();
Char * pchar = (char *) T;
String result = pchar;
Return result;
}
Wstring s2ws (const string & S)
{
_ Bstr_t T = S. c_str ();
Wchar_t * pwchar = (wchar_t *) T;
Wstring result = pwchar;
Return result;
}
Method 3: Use the mbstowcs () function of the CRT Library and the wcstombs () function. The platform has nothing to do with it. Set locale.
# Include <string>
# Include <locale. h>
Using namespace STD;
String ws2s (const wstring & ws)
{
String curlocale = setlocale (lc_all, null); // curlocale = "C ";
Setlocale (lc_all, "CHS ");
Const wchar_t * _ source = ws. c_str ();
Size_t _ dsize = 2 * ws. Size () + 1;
Char * _ DEST = new char [_ dsize];
Memset (_ DEST, 0, _ dsize );
Wcstombs (_ DEST, _ source, _ dsize );
String result = _ DEST;
Delete [] _ DEST;
Setlocale (lc_all, curlocale. c_str ());
Return result;
}
Wstring s2ws (const string & S)
{
Setlocale (lc_all, "CHS ");
Const char * _ source = S. c_str ();
Size_t _ dsize = S. Size () + 1;
Wchar_t * _ DEST = new wchar_t [_ dsize];
Wmemset (_ DEST, 0, _ dsize );
Mbstowcs (_ DEST, _ source, _ dsize );
Wstring result = _ DEST;
Delete [] _ DEST;
Setlocale (lc_all, "C ");
Return result;
}
// Method 4, standard C ++ conversion method: (to be continued)
// Method 5: Use the C # class library in C ++: (to be continued)
Among them, there are always some problems with my implementation. Fifth, I only know that there is such a solution, and I don't have time to learn it in detail. I 'd like to give some tips.

 

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.