Recently in C + + programming often encountered the need for multibyte characters and wide-byte characters to convert the problem, has been affixed to their own code. Feel the trouble, so I wrote a class to encapsulate the conversion between the wchar_t and the CHAR type, others, such as: cstring\ Lpwstr\tchar char\lpstr
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class Cuser
{
Public
Cuser ();
virtual~ Cuser ();
char* Wchartochar (wchar_t* WC);//wide byte to single byte
wchar_t* Chartowchar (char* c); Single byte width bytes
void release ()/Free resources
Private
char* M_char;
wchar_t* M_wchar;
};
/////////////////////////////////////////////////////////////////////////////////////
/* Character type wchar_t Char
/* Get character length wcslen () strlen ()
/* Connect two strings Wcscat () strcpy ()
/* Copy String wcscpy () strcpy ()
/* Compare two strings wcscmp () strcmp ()
/* Specific parameters for details see www.linuxidc.com*/
////////////////////////////////////////////////////////////////////////////////////
Cuser::cuser ()
: M_char (NULL)
, M_wchar (NULL)
{
}
Cuser::~cuser ()
{
Release ();
}
char* Cuser::wchartochar (wchar_t* WC)
{
Release ();
int len= WideCharToMultiByte (Cp_acp,0,wc,wcslen (WC), null,0,null,null);
M_char=new char[len+1];
WideCharToMultiByte (Cp_acp,0,wc,wcslen (WC), m_char,len,null,null);
M_char[len]= ' ";
return M_char;
}
wchar_t* Cuser::chartowchar (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;
M_char=null;
}
if (M_wchar)
{
Delete M_wchar;
M_wchar=null;
}
}
The use of the time is very simple:
Copy Code code as follows:
wchar* Wc=finddata.cfilename;
Cuser u;
char* C=u.wchartochar (WC);
cout<<c<<endl;
Cuser u;
char* Pbuffer=u.wchartochar (Szfullpath);
cout<<totalnum<< "" <<pBuffer<<endl;