C++整數和IP字符串轉換

 

 

char* UlongToIp(ULONG Num)
{
    //Num = 52406464;
    //return x.x.x.x
    char *IP = NULL;
    unsigned int ip_int_index[4], ip_temp_numbr = 24;
    int i = 0;

    for(i = 0; i < 4; i++)
    {
        ip_int_index[i]=(Num>>ip_temp_numbr)&0xFF;
        ip_temp_numbr-=8;
    }

    if ((IP=(char *)malloc(17*sizeof(char)))==NULL)
    {
        return "ERROR";
    }
        
    sprintf(IP,"%d.%d.%d.%d",ip_int_index[0],ip_int_index[1],ip_int_index[2],ip_int_index[3]);
     
    return IP;
}

 

 

參考:  C语言IP地址与int互转

 

posted @ 2020-12-25 11:59  quanzhan  阅读(149)  评论(0编辑  收藏  举报