又如何将xxx.xxx.xxx.xxx类型的IP地址转换成int型呢?

解决方案 »

  1.   

    char* FAR inet_ntoa(
      struct   in_addr in
    );unsigned long inet_addr(
      const char* cp
    );
      

  2.   

    这样也可以:
    DWORD GetNetworkAddr(const char* addr)
    {
    if(addr == NULL)
    return INADDR_NONE;
    if(addr[0] == 0)
    return INADDR_NONE; DWORD dwAddr = 0;
    DWORD dwPart, dwLoop;
    if(addr[1] == 0) // Unicode
    {
    WCHAR c;
    const WCHAR* cp = (const WCHAR*)addr;
    for(dwLoop = 0; dwLoop < 4; dwLoop++)
    {
    dwPart = 0;
    while ((c = *cp++) != L'\0' && c != L'.') {
    if (c < L'0' || c > L'9') // IP 地址中含有非法字符
    return INADDR_NONE;
    dwPart = dwPart * 10 + (c - L'0');
    }
    if(dwPart > 255) // IP 地址中某个段的内容大于 255
    return INADDR_NONE;
    dwAddr = dwAddr | (dwPart << dwLoop*8);
    if (dwLoop == 3) {
    if (c != L'\0')
    return INADDR_NONE; // 给定的 IP 地址太长
    } else {
    if (c == L'\0')
    return INADDR_NONE; // 给定的 IP 地址太短
    }
    }
    }else{ // ASCII
    char c;
    const char* cp = (const char*)addr;
    for (dwLoop = 0; dwLoop < 4; dwLoop++) {
    dwPart = 0;
    while ((c = *cp++) != '\0' && c != '.' && c != ',') {
    if (c < '0' || c > '9') // IP 地址中含有非法字符
    return INADDR_NONE;
    dwPart = dwPart * 10 + (c - '0');
    }
    if (dwPart > 255) // IP 地址中某个段的内容大于 255
    return INADDR_NONE;
    dwAddr = dwAddr | (dwPart << dwLoop*8);
    if (dwLoop == 3) {
    if (c != '\0' && c != ',')
    return INADDR_NONE; // 给定的 IP 地址太长
    } else {
    if (c == '\0' || c == ',')
    return INADDR_NONE; // 给定的 IP 地址太短
    }
    }
    }
    return dwAddr;
    }
      

  3.   

    我这段代码,经过测试。
    DWORD ip = 16820416;
    in_addr aa;
    aa.S_un.S_addr = ip;
    CString str = inet_ntoa(aa);
    AfxMessageBox(str);    //16820416 是192.168.0.1
    引入头文件#include "Winsock2.h"
    然后设置Project->setting->Object libary modules添加Ws2_32.lib 就可以了注意IP最好是使DWORD或者unsigned int
      

  4.   


        WSADATA wsaData;
        int iRet = WSAStartup(MAKEWORD(0x02, 0x02), &wsaData);
        if (iRet != 0)
        {
            TRACE("初始化winsock动态库出错!");
            m_strIPAddress = "";
            return;
         }      struct in_addr localaddr;
          struct hostent *hp=NULL;
          char hostname[50];
          gethostname(hostname,49);//主机名      hp=gethostbyname(hostname);//主机信息
          memcpy(&localaddr,hp->h_addr,hp->h_length);//地址      m_strIPAddress = inet_ntoa(localaddr);//变成char *      WSACleanup();
      

  5.   

    struct in_addr in;
    in.S_un.s_addr = dwIP;
    CString sIP = inet_ntoa(in);
      

  6.   

    不过题目和内容怎么不一样啊
    题目:如何将int型的IP地址转换成xxx.xxx.xxx.xxx的格式?
    内容:又如何将xxx.xxx.xxx.xxx类型的IP地址转换成int型呢?
    到底想问哪个,还是都想问
      

  7.   

    真麻烦,害的我废了这么多口舌。我头一次在一个帖子上回复那么多次
    u_long ip;
    ip = inet_addr("192.168.0.1");
    CString str;
    str.Format("%d",ip);
    AfxMessageBox(str);
    这是内容所需要的代码,题目所需要的代码已经在上面了,别忘记给我加分,顺便再写一遍我这段代码,经过测试。
    DWORD ip = 16820416;
    in_addr aa;
    aa.S_un.S_addr = ip;
    CString str = inet_ntoa(aa);
    AfxMessageBox(str);    //16820416 是192.168.0.1
    引入头文件#include "Winsock2.h"
    然后设置Project->setting->Object libary modules添加Ws2_32.lib 就可以了注意IP最好是使DWORD或者unsigned long
    需要手算我也可以告诉你,
      

  8.   

    inet_toa();
    有IP类型的组件!
      

  9.   

    inet_ntoa()但是不是int型!是unsigned long型