CString,CStringA,DWORD,CHAR,string,const char*等字符类型间的相互转化

字符类型之间的相互转化(1):

1.CString 转 CStringA

1)  使用UNICODE字符集编程时,可能需要使用到那些不支持UNICODE的库,此时就
    需要将CString转成CStringA.
 方法: 
       CString str1=_T("CSDN");;      
       CStringA str2;
       str2=str1;
2)使用ANSI字符集编程时,CString 和 CStringA 类型相同

2.DWORD转CString(MFC IP控件)

 方法: 
       DWORD m_lIpAddress;
       CString strIpAddress;
       strIpAddress.Format(_T("%d.%d.%d.%d"),((BYTE*) &m_lIpAddress)[3],((BYTE*)&m_lIpAddress)[2],((BYTE *)&m_lIpAddress)[1],((BYTE *)&m_lIpAddress)[0]);

3.CString转DWORD(MFC IP控件)

 方法:先将CString转成CStringA,再转化成DWORD
       CString m_strRemoteIpAddr;
	   CStringA Str2;
	   DWORD  dwIP;
	   Str2 = m_strRemoteIpAddr;
	   dwIP = inet_addr(Str2.GetBuffer());
	   unsigned char*pIP = (unsigned char*)&dwIP;
	   m_strRemoteIP.SetAddress(*pIP, *(pIP + 1),*(pIP + 2),*(pIP + 3));

4.CString转CHAR

 方法:
	char* CStringToChar(CString strSrc)
	{
		#ifdef UNICODE  
			DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, strSrc.GetBuffer(0), -1, NULL, 0, NULL, FALSE);
			strSrc.ReleaseBuffer();
			char *psText;
			psText = new char[dwNum];
			if (!psText)
				delete[]psText;
			WideCharToMultiByte(CP_OEMCP, NULL, strSrc.GetBuffer(0), -1, psText, dwNum, NULL, FALSE);
			strSrc.ReleaseBuffer();
			return psText;
		#else  
			LPCTSTR p = strSrc.GetBuffer(0);
			strSrc.ReleaseBuffer();
			char *psText;
			psText = new char[strSrc.GetLength() + 1];
			strcpy_s(psText, strSrc.GetLength() + 1, CT2CA(p));
			if (!psText)
				delete[]psText;
			return psText;
			//	return (LPCTSTR)strSrc;  
		
		#endif  
	}

5.string转CString

 方法:
        string strTest = "CSDN";
	    CString cstrTest;
	    cstrTest = CA2A(strTest.c_str());

6.CString转string

 方法:
        CString cstrTest = _T("CSDN");
	    string strTest;
	    strTest = CT2A(cstrTest.GetString());

7.const char*转CString

 方法:
	   const char* cstrTest = "CSDN";
	   CString strTest(cstrTest);

8.CString转const char*

 方法:
      CString cstrTest  = _T("CSDN");
	  USES_CONVERSION;
	  std::string str(W2A(cstrTest ));
	  const char* cstr = str.c_str();

9.string转const char*

 方法:
      string str = "CSDN";
	  const char* cstr = str.c_str();

10.const char*转string

 方法:
      const char* cstr = "CSDN";
	  string str(cstr);
代码优化下面的代码:DWORD WINAPI SoapServerThread( LPVOID pParam ) { CUserManagerServerDlg *pUserManServer = (CUserManagerServerDlg *)pParam; struct soap Soap_service; soap_init(&Soap_service); Soap_service.pUserManagerServerDlg = pUserManServer; Soap_service.bind_flags = SO_REUSEADDR; USES_CONVERSION; std::string strIP(W2A(pUserManServer->m_csIP)); const char* chIP = strIP.c_str(); CString csInfo; int nBindErr; if ((nBindErr = soap_bind(&Soap_service,chIP, pUserManServer->m_nPort, 100))< 0) //soap_bind(&math_service, address, port, 100); { soap_print_fault(&Soap_service, stderr); //csInfo.Format(_T("用户服务器绑定失败")); csInfo.LoadString(IDS_BIND_FAILD); pUserManServer->RecordOperaInfo(csInfo,6); pUserManServer->RecordSQL(csInfo,6); return -1; } if (pUserManServer->m_csIP.IsEmpty() || !pUserManServer->m_nPort) { //csInfo.Format(_T("IP和端口号不允许为空!")); csInfo.LoadString(IDS_CANNOT_EMPTY); pUserManServer->RecordOperaInfo(csInfo,6); pUserManServer->RecordSQL(csInfo,6); return -1; } //csInfo.Format(_T("用户服务器已启动...")); csInfo.LoadString(IDS_STARTED); pUserManServer->RecordOperaInfo(csInfo,0); pUserManServer->RecordSQL(csInfo,0); CString csListen; csListen.LoadString(IDS_LISTEN); csInfo.Format(_T("%s:IP(%s),Port:(%d)"),csListen,pUserManServer->m_csIP,pUserManServer->m_nPort); pUserManServer->RecordOperaInfo(csInfo,0); pUserManServer->RecordSQL(csInfo,0); while (true) { int nSlave = (int)soap_accept(&Soap_service); if (nSlave < 0) { //csInfo.Format(_T("soap_accept调用失败...")); csInfo.LoadString(IDS_CALL_FAILD); pUserManServer->RecordOperaInfo(csInfo,6); pUserManServer->RecordSQL(csInfo,6); soap_print_fault(&Soap_service, stderr); return -1; } soap_serve(&Soap_service); soap_destroy(&Soap_service); soap_end(&Soap_service); } return 0; }
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值