Deep understanding of the conversion _c language between char* and wchar_t* and string and wstring in C + +

Source: Internet
Author: User
Copy Code code as follows:

#ifndef Use_h_
#define Use_h_

#include <iostream>
#include <windows.h>
#include <string>
using namespace Std;
Class Cuser
Cuser ();
virtual~ Cuser ();
char* Wchartochar (const wchar_t* WP);
char* Stringtochar (const string& s);
char* Wstringtochar (const wstring& WS);
wchar_t* Chartowchar (const char* c);
wchar_t* Wstringtowchar (const wstring& WS);
wchar_t* Stringtowchar (const string& s);
Wstring stringtowstring (const string& s);
String wstringtostring (const wstring& WS);
void release ();
char* M_char;
wchar_t* M_wchar;

Copy Code code as follows:

#include "stdafx.h"
#include "use.h"
Cuser::cuser ()
: M_char (NULL)
, M_wchar (NULL)
Cuser::~cuser ()
Release ();
char* Cuser::wchartochar (const wchar_t* WP)
Release ();
int len= WideCharToMultiByte (Cp_acp,0,wp,wcslen (WP), null,0,null,null);
M_char=new char[len+1];
WideCharToMultiByte (Cp_acp,0,wp,wcslen (WP), m_char,len,null,null);
M_char[len]= ' ";
return M_char;
wchar_t* Cuser::chartowchar (const char* c)
Release ();
int len = MultiByteToWideChar (Cp_acp,0,c,strlen (c), null,0);
M_wchar=new wchar_t[len+1];
MultiByteToWideChar (Cp_acp,0,c,strlen (c), M_wchar,len);
M_wchar[len]= ' ";
return M_wchar;
void Cuser::release ()
if (M_char)
Delete M_char;
if (M_wchar)
Delete M_wchar;
char* Cuser::stringtochar (const string& s)
Return const_cast<char*> (S.c_str ());
char* Cuser::wstringtochar (const std::wstring &AMP;WS)
Const wchar_t* WP=WS.C_STR ();
return Wchartochar (WP);
wchar_t* Cuser::wstringtowchar (const std::wstring &AMP;WS)
Return const_cast<wchar_t*> (Ws.c_str ());
wchar_t* Cuser::stringtowchar (const string& s)
Const char* P=S.C_STR ();
Return Chartowchar (P);
String cuser::wstringtostring (const std::wstring &AMP;WS)
string S;
char* P=wstringtochar (WS);
S.append (P);
return s;
Wstring cuser::stringtowstring (const std::string &s)
Wstring ws;
wchar_t* Wp=stringtowchar (s);
Ws.append (WP);
return WS;

If it involves CString, you can use the Cstring::getbuffer () function to convert it.
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: 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.