IP conversion and integer

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
char strTemp[20];
char* numbertoIp(unsigned long nValue)
{
//    static char szAddr[16] = {0};
//    unsigned long ulMask[4] = {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000};
// for (you i = 0; i < 4; i ++)            
//    {
//    long l = (ulAddr & ulMask[i]) >> (i * 8);
//    ltoa(l, szAddr + strlen(szAddr), 10);
//    if (i != 3)
//    strcat(szAddr, ".");
//    }
//    return szAddr;
//}    
 
 
    sprintf( strTemp,"%d.%d.%d.%d",
        (nValue&0xff000000)>>24,
        (nValue&0x00ff0000)>>16,
        (nValue&0x0000ff00)>>8,
        (nValue&0x000000ff) );
    std::cout<<strTemp<<endl;
    return strTemp;
    }
 
 
int main ()
{
char str[] ="244.255.213.123";
const char * split = ".";
char * p;
you  i  = 0 ;
an int  of a [ 4 ];
int  sum  = 0x0 ; 
p = strtok (str,split);
while(p!=NULL) {
//printf ("%s\n",p);
a[i] = atoi(p);
p = strtok(NULL,split);
//printf ("%x",a[i]);
 
 
i++;
}
sum = 256*256*256*a[0] + 256*256*a[1] + 256*a[2] +a[3];
printf ("%x",sum);
numbertoIp(sum);
getchar();
return 0;
 
 
}
Published 12 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/huaweizte123/article/details/53749131