CString、char*l类型转换

环境:VS2010

1.CString to char*

第一种方法:

需头文件:atlconv.h

    CString   host_string;     
//CString to char* USES_CONVERSION; char *ipaddress = T2A(host_string);

此方法消耗栈空间,适合少量使用。

第二种方法:

int My_WcharToChar(char* pDest,CString& pSource)
{
 wchar_t* pawstr = NULL;
 pawstr = pSource.GetBuffer(pSource.GetLength()+1);
 wcstombs(pDest,pawstr,pSource.GetLength()+1);
 return   TRUE; 
}

demo:

#include "afxcmn.h"
#include "afxwin.h"
#include "stdio.h"
#include "stdlib.h"

//vs2010编译通过

int My_WcharToChar(char* pDest,CString& pSource);
void main()
{
    CString csMsg = _T("abcdefghigklmnopqrst");
    char chMsg[100];
    My_WcharToChar(chMsg, csMsg);
    printf("GetLength:%d\n",csMsg.GetLength());
    printf("sizeof:%d\n",sizeof(csMsg));
    printf("%s\n", chMsg);
    printf("sizeof:%d\n", sizeof(chMsg));
    printf("strlen:%d\n", strlen(chMsg));
}

int My_WcharToChar(char* pDest,CString& pSource)
{
 wchar_t* pawstr = NULL;
 pawstr = pSource.GetBuffer(pSource.GetLength()+1);
 wcstombs(pDest,pawstr,pSource.GetLength()+1);
 return   TRUE;
}

 

2.

posted @ 2013-07-11 21:35  风风清清扬扬  阅读(335)  评论(0编辑  收藏  举报