将const wchar_t*转换为const char*。

时间:2021-03-02 19:28:27

I am trying to use GetHostByName() this requires a const char*. I have my URL in a variable that is in a cost wchar_t* format. How can I convert this so that GetHostByName may use it? The code.

我正在尝试使用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;

This is the error I am getting. IntelliSense:argument of type "const wchar_t *" is incompatible with parameter of type "const char *"

这是我的错误。智能感知:类型“const wchar_t *”的参数与类型“const char *”的参数不兼容。

4 个解决方案

#1


4  

I like to use wcstombs() because it is pretty easy to use.

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

Try this sample:

试试这个示例:

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

This is how you have to convert wchar_t into char*.

这就是如何将wchar_t转换为char*的方法。

EDIT

编辑

Changes in your code:

改变你的代码:

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

#2


1  

This seems to work. Comments welcome.

这似乎工作。欢迎评论。

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


0  

WideCharToMultiByte is the Win32 API call that does this at the end of the day, though depending on what frameworks you're using (MFC, WTL, etc), there may be a better way.

WideCharToMultiByte是一种Win32 API调用,它在一天结束时进行,不过取决于您使用的是什么框架(MFC、WTL等),可能会有更好的方法。

#4


0  

    /***** 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];
    }

#1


4  

I like to use wcstombs() because it is pretty easy to use.

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

Try this sample:

试试这个示例:

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

This is how you have to convert wchar_t into char*.

这就是如何将wchar_t转换为char*的方法。

EDIT

编辑

Changes in your code:

改变你的代码:

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

#2


1  

This seems to work. Comments welcome.

这似乎工作。欢迎评论。

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


0  

WideCharToMultiByte is the Win32 API call that does this at the end of the day, though depending on what frameworks you're using (MFC, WTL, etc), there may be a better way.

WideCharToMultiByte是一种Win32 API调用,它在一天结束时进行,不过取决于您使用的是什么框架(MFC、WTL等),可能会有更好的方法。

#4


0  

    /***** 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];
    }