IP格式转换高手帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/05/24 18:28:22
我有一组双精度数值型IP数据,麻烦大家帮忙转换成普通的IP格式。
起始IP:976340728 结束IP:976340728
3723005442 3723005442
3723020125 3723020125
IP:58.49.195.15大概的位置在武汉那里?

IP地址格式转换在网络编程中会经常遇到,下面总结一下:

UINT->LPSTR

//将一个具有网络字节序的UINT型IP地址转化为点分十进制的字符串形式的IP

//方法1(很简单,不用解释了)

char *IpToString(char *ip, unsigned long lIp)
{
char octeto[4];
ip[0] = 0;
itoa(lIp & 0xff, octeto, 10);
strcat(ip, octeto);
strcat(ip, ".");

itoa((lIp >> 8) & 0xff, octeto, 10);
strcat(ip, octeto);
strcat(ip, ".");

itoa((lIp >> 16) & 0xff, octeto, 10);
strcat(ip, octeto);
strcat(ip, ".");
itoa((lIp >> 24) & 0xff, octeto, 10);
strcat(ip, octeto);

return ip;
}
//方法2
char * IpToString(char *ip, unsigned long lIp)
{

struct in_addr in; //struct in_addr是IP地址结构体
in.S_un.S_addr=lIp;
ip=inet_ntoa(in); //inet_ntoa功能:将struct in_addr形式的IP地址转化为LPSTR型

return ip;
}

LPSTR->UINT

//将一个点分十进制的字符串形式的IP转化为具有网络字节序的UINT型