字符串IP和数字IP互转代码

来源:互联网 发布:破解压缩文件密码软件 编辑:程序博客网 时间:2024/06/18 07:41
void CIpToolsDlg::OnInttocharbutton()
{
    struct stIP
    {
        unsigned char a;
        unsigned char b;
        unsigned char c;
        unsigned char d;
    }ip;

        
    this->UpdateData (TRUE);
    memcpy(&ip,&this->m_IntIp ,sizeof(int));

    this->m_CharIp .Format ("%d.%d.%d.%d",ip.a,ip.b,ip.c,ip.d);


    this->UpdateData (FALSE);
    

    
}

void CIpToolsDlg::OnButtonchartoint()
{
    struct stIP
    {
        unsigned char a;
        unsigned char b;
        unsigned char c;
        unsigned char d;
    }ip;
    
    this->UpdateData (TRUE);
    int oldtop=0;
    int rf=this->m_CharIp .Find ('.',0);
    if(rf==-1){AfxMessageBox("ip erro1");return;}
    ip.a=atoi(m_CharIp.Left (rf).GetBuffer (0));
    oldtop=rf;

    rf=this->m_CharIp .Find ('.',rf+1);
    if(rf==-1){AfxMessageBox("ip erro2");return;}
    ip.b=atoi(m_CharIp.Mid(oldtop+1,rf-oldtop-1).GetBuffer (0));

    oldtop=rf;
    rf=this->m_CharIp .Find ('.',rf+1);
    if(rf==-1){AfxMessageBox("ip erro3");return;}
    ip.c=atoi(m_CharIp.Mid(oldtop+1,rf-oldtop-1).GetBuffer (0));
    ip.d=atoi(m_CharIp.Right (m_CharIp.GetLength ()-rf-1));

    memcpy(&m_IntIp ,&ip,sizeof(int));


    this->UpdateData (FALSE);
    
    
    // TODO: Add your control notification handler code here
    
}

原创粉丝点击