【问题标题】:Convert const wchar_t* to const char*将 const wchar_t* 转换为 const char*
【发布时间】:2011-11-09 02:33:42
【问题描述】:

我正在尝试使用 GetHostByName() 这需要一个 const char*。我的 URL 位于成本 wchar_t* 格式的变量中。如何转换它以便 GetHostByName 可以使用它?代码。

BSTR bstr;
pBrowser->get_LocationURL(&bstr);
std::wstring wsURL;
wsURL = bstr;

size_t DSlashLoc = wsURL.find(L"://");
if (DSlashLoc != wsURL.npos)
    {
    wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
    }
DSlashLoc = wsURL.find(L"www.");
if (DSlashLoc == 0)
    {
    wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
    }
DSlashLoc = wsURL.find(L"/");
if (DSlashLoc != wsURL.npos)
    {
    wsURL.erase(DSlashLoc);
    }
    wprintf(L"\n   Current Website URL: %s\n\n", wsURL.c_str());

    HOSTENT *pHostEnt;
    int  **ppaddr;
    SOCKADDR_IN sockAddr;
    char* addr;
    pHostEnt = gethostbyname(wsURL.c_str());
    ppaddr = (int**)pHostEnt->h_addr_list;
    sockAddr.sin_addr.s_addr = **ppaddr;
    addr = inet_ntoa(sockAddr.sin_addr);
    printf("\n   Current Website IP:%s", addr);

int length = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0,  NULL, NULL); 
std::string LogURL(length+1, 0); 
int result = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &LogURL[0],length+1,  NULL, NULL);
myfile << "\n   Current Website URL:" << LogURL;
myfile << "\n   Current Website IP:"<< addr;

这是我得到的错误。 IntelliSense:“const wchar_t *”类型的参数与“const char *”类型的参数不兼容

【问题讨论】:

  • 正如目前所写的,您正在尝试将 wsURL.c_str() 传递给 gethostbyname 函数。你不想传递 LogURL.c_str() 吗?

标签: windows visual-c++


【解决方案1】:

我喜欢使用 wcstombs(),因为它很容易使用。

试试这个示例:

char *str = new char[4046];
wchar_t array[] = L"Hello World";
wcstombs(str, array, 12);
std::cout << str;

这就是您必须将 wchar_t 转换为 char* 的方法。

编辑

代码更改:

char* addr = new char[4046];
wcstombs(wsURL, addr, wsURL.size());
pHostEnt = gethostbyname(addr);

【讨论】:

  • 我在看这个,我不确定你的意思是什么?您能否在我的上下文中进一步解释,以便我理解。谢谢。
  • @ME:描述模板类 basic_string 的特化的类型,其中 wchar_t 类型的元素作为 wstring。
  • @ME:所以我们可以使用 wcstombs 函数将 wchar_t 转换为 const char*。在我回答的编辑部分,我建议您的问题的解决方案。
  • 这一行导致转换问题...wcstombs(wsURL, addr, wsURL.length());
  • @ME:第三个参数是wsURL字符串的长度。所以你必须计算长度并将该值作为第三个参数传递
【解决方案2】:

这似乎有效。欢迎评论。

int Newlength = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0,  NULL, NULL);
std::string NewLogURL(Newlength+1, 0); 
int Newresult = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &NewLogURL[0],Newlength+1,  NULL, NULL);

    HOSTENT *pHostEnt;
    int  **ppaddr;
    SOCKADDR_IN sockAddr;
    char* addr;

    pHostEnt = gethostbyname(NewLogURL.c_str());
    ppaddr = (int**)pHostEnt->h_addr_list;
    sockAddr.sin_addr.s_addr = **ppaddr;
    addr = inet_ntoa(sockAddr.sin_addr);
    printf("\n   Current Website IP:%s", addr);

【讨论】:

    【解决方案3】:
        /***** This code is well done *****/
    
        #include...
        #include...
    
        int wmain(int argc, wchar_t *argv[])
        {
             ...
             ...
             char *path = new char[255];
             wcstombs(path, argv[2], 255);
             IplImage *img; 
       if (img = cvLoadImage (path, 1))
             {
                 Mat input_img = Mat (img);
                 imshow ("haha",input_img);
                 waitKey(0);
             }
             ...
             ...
             //wcout<<endl<<argv[2];
        }
    

    【讨论】:

      【解决方案4】:

      WideCharToMultiByte 是在一天结束时执行此操作的 Win32 API 调用,但根据您使用的框架(MFC、WTL 等),可能会有更好的方法。

      【讨论】:

      • 这里没有 MFC。这就是我在代码中的进一步内容,它可以工作。但是,当我尝试使用 LogURL 时,我无法将 std 字符串转换为 const char。 int 长度 = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0, NULL, NULL); std::string LogURL(长度+1, 0); int 结果 = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &LogURL[0],length+1, NULL, NULL); myfile
      • Yee Gads,很抱歉,无法格式化!看我加的问题,上面比较清楚。
      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 2017-03-31
      • 2011-12-11
      • 2012-08-04
      • 1970-01-01
      • 2013-08-11
      • 2011-12-27
      相关资源
      最近更新 更多