C + + multibyte characters and wide byte characters convert to _c language

Source: Internet
Author: User
Tags strlen
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;
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.