CString与char的相互转换

来源:互联网 发布:java 接收post文件 编辑:程序博客网 时间:2024/06/08 19:33
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;
}
0 0
原创粉丝点击