bool IP2Num(const char* ip,struct in_addr& addr)
{
    for(int i=0;i<strlen(ip);++i)
    {
        if(\'.\'==ip[i])continue;
        if(\'0\'<=ip[i]&&\'9\'>=ip[i])continue;
        return false;
    }

    int ch[4]={0};
    if(4!=sscanf(ip,"%d.%d.%d.%d",&ch[0],&ch[1],&ch[2],&ch[3]))return false;
    addr.S_un.S_addr=0;

    addr.S_un.S_addr+=(ch[3]&0xff);
    addr.S_un.S_addr=addr.S_un.S_addr<<8;

    addr.S_un.S_addr+=(ch[2]&0xff);
    addr.S_un.S_addr=addr.S_un.S_addr<<8;

    addr.S_un.S_addr+=(ch[1]&0xff);
    addr.S_un.S_addr=addr.S_un.S_addr<<8;

    addr.S_un.S_addr+=(ch[0]&0xff);
    return true;
}

 

版权声明:本文为yuandaozhe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yuandaozhe/p/10073887.html