USES_CONVERSION

宽窄字符的转换对于C++专业户来说是经常的动作。一般两种比较方便的方法:

  1. C++:WideCharToMultiByte MultiByteToWideChar; C语言提供的转换函数为mbstowcs()/wcstombs()

  2. C++:A2W W2A (USES_CONVERSION)

  3. (追加)CStringA CStringW 之间仅用一个"="就完成了转换 神奇的很!!Love CString, Love Programming!

Method 1: #include <windows.h>

Method 2: #include <atlconv.h> 且使用A2W/W2A前一定要写USES_CONVERSION

Test Func:

void Func17()
{
    //测试W2A时是否需要一倍的空间
    cout<<"------------------------testing _W2A_------------------------"<<endl;
    CStringW cW = "nVIDIA";
    int nLenW = cW.GetLength();
    cout<<"W length:  "<<nLenW<<endl;
    wcout<<L"W string:   "<<cW.GetBuffer()<<endl;

    int nLenA = nLenW*2+1;

    char* szA = new char[nLenA];
    memset(szA, 0, nLenA);
    
    cout<<"A length before translating:  "<<nLenA<<endl;

    WideCharToMultiByte(CP_ACP, 0, cW.GetBuffer(), -1, szA, nLenA, NULL, NULL);  //CP_ACP when to use other ?

    cout<<"A length after translated:  "<<strlen(szA)<<endl;
    cout<<"A string:  "<<szA<<endl;

    free(szA);
    szA = NULL;


    cout<<"--------------------------testing _A2W_----------------------------------"<<endl;
    CStringA cA = "Intel Chipset";
    int nLenA2 = cA.GetLength();
    cout<<"A length:  "<<nLenA2<<endl;
    cout<<"A string:   "<<cA.GetBuffer()<<endl;

    cout<<endl;

    int nLenW2 = nLenA2 + 1;     //  '\0' plus
    wchar_t* szW = new wchar_t[nLenW2];
    memset(szW, 0, nLenW2);
    cout<<"W length b4 translating:   "<<nLenW2<<endl;
    MultiByteToWideChar(CP_ACP, 0, cA.GetBuffer(), -1, szW, nLenW2);
    cout<<"W length a4 translated:    "<<wcslen(szW)<<endl;
    wcout<<"W string:    "<<szW<<endl;

    free(szW);
    szW = NULL;

    cout<<"----------------------testing   USES_CONVERSION----------------"<<endl;
    CStringA cA3 = "Grok those URLs";
    cout<<"cA3 b4:  "<<cA3.GetBuffer()<<endl;

    USES_CONVERSION;
    CStringW cW3 = A2W(cA3.GetBuffer());
    wcout<<cW3.GetBuffer()<<endl;

    cA3 = W2A(cW3.GetBuffer());
    cout<<"cA3 a4:   "<<cA3.GetBuffer()<<endl;  //yeah~

}

但要注意:使用USES_CONVERSION是从堆栈上分配的内存,如果这么写:

void fn()
{
    while(true)
    {
        {
            USES_CONVERSION;
            DoSomething(A2W("SomeString"));
        }
    }
}

很可能导致stack overflow.

参考:http://www.cnblogs.com/carekee/articles/1935789.html

(追加)

cout<<"----------------------CStringA  <--> CStringW can translated automatically----------------"<<endl;
    CStringW cW4= "Love CString, Love Programming";
    CStringA cA4 = cW4;
    wcout<<"W string:  "<<cW4.GetBuffer()<<endl;
    cout<<"A string:     "<<cA4.GetBuffer()<<endl;

    cout<<endl<<endl;

    cW4 = cA4;
    wcout<<"W string :    "<<cW4.GetBuffer()<<endl;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码优化下面的代码: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、付费专栏及课程。

余额充值