/*
功能: 简单的wchar_t 和 char 转换类
*/
class CWCharToChar
{
protected:
char * m_cDest;
wchar_t * m_wcDest;
bool m_bSrcIsChar;
public:
char * Char(void){ return m_cDest; }
wchar_t * WChar(void){ return m_wcDest; }
CWCharToChar(const char* psrc)
: m_cDest(NULL)
, m_wcDest(NULL)
, m_bSrcIsChar(true)
{
m_cDest = (char*)psrc;
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, m_cDest, -1, NULL, 0);
m_wcDest = new wchar_t[dwNum];
MultiByteToWideChar (CP_ACP, 0, m_cDest, -1, m_wcDest, dwNum);
}
CWCharToChar(const wchar_t * psrc)
: m_cDest(NULL)
, m_wcDest(NULL)
, m_bSrcIsChar(false)
{
m_wcDest = (wchar_t*)psrc;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL, m_wcDest,-1,NULL,0,NULL,FALSE);
m_cDest = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,m_wcDest,-1,m_cDest,dwNum,NULL,FALSE);
}
~CWCharToChar()
{
if( m_bSrcIsChar && m_wcDest ) delete [] m_wcDest;
if( !m_bSrcIsChar && m_cDest ) delete [] m_cDest;
}
};
例子
wchat_t tDbName[64] = { 0 };
wsprintf(tDbName, _T("use Happy"));
CWCharToChar dbname(tDbName);
CWCharToChar a(dbname.Char());
int ret = wcscmp(a.WChar(), tDbName );
printf("%d\n", ret);
简单的wchar_t 和 char 转换类
最新推荐文章于 2022-11-16 22:25:46 发布