本文代码完成"192.168.1.1"这类的字符串转换到struct in_addr中存储 // TCAHR to char BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize) { DWORD dwMinSize; dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE); if(dwSize < dwMinSize) { return FALSE; } WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE); return TRUE; } ////////////////////////////////////////////////////////////////// char temp[18]; DWORD dwAddr; struct in_addr devIP; TCHAR szIP[] = _T("192.168.1.1"); if (WCharToMByte(szIP,temp,18)) // TCHAR* 2 char* { dwAddr = ntohl(inet_addr(temp)); // devIP.S_un.S_addr = htonl(dwAddr); // 完成转换 } ////////////////////////////////////////////////////////////////////////

评论