CString CharToCString(char *pText)
{
int iLength = 0;
wchar_t *pwText = NULL;
CString csText = _T("");
/* Get the number of wide character array */
iLength = MultiByteToWideChar(CP_ACP, 0x00, pText, -1, NULL, 0);
if ( iLength > 0 )
{
/* Allocate wide character array */
pwText = new wchar_t[iLength];
if ( pwText != NULL )
{
/* Convert to ASCII */
MultiByteToWideChar(CP_ACP, 0x00, pText, -1, pwText, iLength);
/* Convert to CString */
csText = pwText;
/* Release memory */
delete []pwText;
pwText = NULL;
}
}
return csText;
}
char* CStringToChar(CString csBuffer)
{
int iLen = 0;
char *pBuf = NULL;
/** Convert wide char into char */
iLen = WideCharToMultiByte(CP_ACP, NULL, csBuffer, -1, NULL, 0, NULL, FALSE);
/** Create char pointer space */
pBuf = new char[iLen];
if ( pBuf != NULL )
{
WideCharToMultiByte(CP_ACP, NULL, csBuffer, -1, pBuf, iLen, NULL, FALSE);
}
return pBuf;
}
{
int iLength = 0;
wchar_t *pwText = NULL;
CString csText = _T("");
/* Get the number of wide character array */
iLength = MultiByteToWideChar(CP_ACP, 0x00, pText, -1, NULL, 0);
if ( iLength > 0 )
{
/* Allocate wide character array */
pwText = new wchar_t[iLength];
if ( pwText != NULL )
{
/* Convert to ASCII */
MultiByteToWideChar(CP_ACP, 0x00, pText, -1, pwText, iLength);
/* Convert to CString */
csText = pwText;
/* Release memory */
delete []pwText;
pwText = NULL;
}
}
return csText;
}
char* CStringToChar(CString csBuffer)
{
int iLen = 0;
char *pBuf = NULL;
/** Convert wide char into char */
iLen = WideCharToMultiByte(CP_ACP, NULL, csBuffer, -1, NULL, 0, NULL, FALSE);
/** Create char pointer space */
pBuf = new char[iLen];
if ( pBuf != NULL )
{
WideCharToMultiByte(CP_ACP, NULL, csBuffer, -1, pBuf, iLen, NULL, FALSE);
}
return pBuf;
}
本文详细介绍了C++中将字符数组转换为CString对象及反之的操作,包括使用MultiByteToWideChar和WideCharToMultiByte函数进行字符集之间的转换,并讨论了内存管理和资源释放的重要性。
1052

被折叠的 条评论
为什么被折叠?



